[Python-3000] Nested Iteration?

Nick Coghlan ncoghlan at gmail.com
Wed Oct 4 13:04:44 CEST 2006


Calvin Spealman wrote:
> I'm sure this has been brought up before, either for Py3K or in
> previous development, so just let me know if its already been
> shotdown. Maybe even give it a second thought.
> 
> for i in some_list in some_list_of_lists:
>     do_something_with(i)
> 
> Currently, this is passed iterating over the results of (some_list in
> some_list_of_lists), which shouldn't ever reveal an iterable result,
> so is there any danger in making the for loops, listcomps, and genexps
> take special meaning to this chaining of iterators?
> 
> This might even remove a great number of the ever-repeating "Why isn't
> there a standard flatten function?" debates, because it would provide
> a simple, intuitive, understandable base for most any way you would
> want to flatten things.

Unfortunately this "simple, intuitive, understandable base" is wrong for the 
same reason most naive flattening functions are wrong - flattening operations 
typically want to treat strings as atomic data rather than as a container 
(although a previous experiment with making strings not behave like a 
container at all for Py3k showed that to be even *more* inconvenient).

If you really want something like this, you can already use a genexp to 
similar effect:

  for i in (some_list for some_list in some_list_of_lists):
      do_something_with(i)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list