Skip to content Skip to sidebar Skip to footer

Generators And Yield Statement

Suppose I want to create a function that takes an iterable. That iterable may contain other iterables to any level. I want to create a function that goes over these in order. So fo

Solution 1:

because when you recurse, you return a new generator -- But that generator never yields anything because you don't iterate over it. Instead, do something like:

defit(l):
  for i in l:
    ifisinstance(i, collections.Iterable):
      for item in it(i):
        yield item
    else:
      yield i

Or, in python3.3, you can use the yield from keyword.

Post a Comment for "Generators And Yield Statement"