-1

I have an input to my code in the form of space separated integers (example, 1 20 92 11). Commonly I would read this into a list using list(map(int, input().split())).

However, I do not want to allocate a list, and simply want to read in numbers one at a time. Pseudo code looks something like this:

def solve():
    num = read_num()
    do_something_with_num()

How do I do this?

petezurich
  • 7,683
  • 8
  • 34
  • 51
adi
  • 31
  • 2
  • 5
  • `input()` reads in entire string -- what is your use case? Why can't you use a `for` loop or `map()` instead? – BrokenBenchmark May 24 '22 at 18:41
  • 3
    it looks like you answered question yourself. I think best bet would be to use `map` as iterator without `list` as above. – rv.kvetch May 24 '22 at 18:43
  • Does this answer your question? [Is there a generator version of \`string.split()\` in Python?](https://stackoverflow.com/questions/3862010/is-there-a-generator-version-of-string-split-in-python) – fsimonjetz May 24 '22 at 18:46
  • Yes, iterating over map object makes sense. Thanks! – adi May 24 '22 at 19:06

0 Answers0