L = [1, 2, 3]
it = iter(L)
it
<...iterator object at ...>
it.__next__() # same as next(it)
1
next(it)
2
next(it)
3
next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
for i in iter(obj):
print(i)
for i in obj:
print(i)
Comprehension
# Generator expression -- returns iterator
stripped_iter = (line.strip() for line in line_list)
# List comprehension -- returns list
stripped_list = [line.strip() for line in line_list]