How does an iterator in a nonempty sequence without filtering and without aggregation ( sum() , etc.) produce nothing?
Consider a simple example:
sequence = ['a', 'b', 'c'] list((el, ord(el)) for el in sequence)
This gives [('a', 97), ('b', 98), ('c', 99)] , as expected.
Now just replace ord(el) out with an expression that extracts the first value from some generator using (...).next() - forgives a far-fetched example:
def odd_integers_up_to_length(str): return (x for x in xrange(len(str)) if x%2==1) list((el, odd_integers_up_to_length(el).next()) for el in sequence)
This gives [] . Yes, an empty list. No ('a', stuff ) tuples. Nothing.
But we do not filter, do not aggregate and do not reduce. A generator expression over n objects without filtering or aggregation should give n objects, right? What's happening?
source share