How to define and use Python generators appropriately
Posted
by Morlock
on Stack Overflow
See other posts from Stack Overflow
or by Morlock
Published on 2010-04-07T03:25:23Z
Indexed on
2010/04/07
3:33 UTC
Read the original article
Hit count: 376
I want to define a generator from a list that will output the elements one at a time, then use this generator object in an appropriate manner.
a = ["Hello", "world", "!"]
b = (x for x in a)
c = next(b, None)
while c != None:
print c,
c = next(b, None)
Is there anything wrong or improvable with the while
approach here? Is there a way to avoid having to assign 'c' before the loop?
Thanks!
© Stack Overflow or respective owner