for loop without variable

Paul Rubin http
Fri Jan 11 06:11:08 CET 2008

Mike Meyer <mwm-keyword-python.b4bdba at> writes:
> data_out = [[] for _ in data_in]
> ...
> But I'm curious - what's the difference between the "foreach" you have
> in mind and the standard python "for"?

The "for" loop, like the list comprehension, pollutes the namespace
with an index variable that's not used for anything.  I prefer genexps:

    data_out = list([] for x in data_in)

More information about the Python-list mailing list