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!
Why would you use a
whileloop? In Python,forloops are absolutely designed for this:If you are stuck on a
whileimplementation for whatever reason, your current implementation is probably the best you can do, but it’s a bit clunky, don’t you think?