How can an iterator over a non-empty sequence, with no filtering and no aggregation (sum()
, etc.), yield nothing?
Consider a simple example:
sequence = ['a', 'b', 'c']
list((el, ord(el)) for el in sequence)
This yields [('a', 97), ('b', 98), ('c', 99)]
as expected.
Now, just swap the ord(el)
out for an expression that takes the first value out of some generator using (...).next()
— forgive the contrived 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 yields []
. Yeah, empty list. No ('a',
stuff)
tuples. Nothing.
But we're not filtering or aggregating or reducing. A generator expression over n
objects without filtering or aggregation must yield n
objects, right? What's going on?