No, the whole point of an iterator is that it returns one match at a time. The iterator is useful when you want to process one match at a time in isolation, precisely because it avoids returning more than you actually consume. If you bail out after processing the first match, the iterator will never even reach the point of searching for the second.
If you store all the matches in a list in the caller, probably simply use findall
instead of finditer
, as you are foregoing the possible benefits.
The memory address which Python returns is just the address of the iterator object. Every object in Python has an address in memory, though some are less parsimonious about what exactly is being stored. This is just Python's default repr
of internal objects; it includes the memory address mainly as a unique identifer, so that a human reader can tell whether you printed the same object twice, or two different objects of the same type.
Storing the iterator in a variable is sometimes useful, though in this case, it looks like you expected it to contain the matches, not the iterator. This is a common beginner error with iterators and generators.
Understanding the yield
keyword should give you a basic grip on generators, and thus iterators. Perhaps review What does the "yield" keyword do?
In so many words, Python keeps track of the state of the iterator between calls. The next time you call it, it recalls where it stopped searching the previous time, and continues searching from there.
You could do the same thing without yield
, too. Here is a simple Python example which returns a function which returns an item at a time from a list.
def stupid_iterator(seq):
"""Make an iterator for seq"""
def _iter():
return seq.pop(0)
return _iter
i = stupid_iterator([1, 2, 3])
print(i) # for fun; notice memory address in repr
print(i())
print(i())
print(i())
print(i()) # fails: list is empty
Demo: https://ideone.com/qc5XfQ