Single-level list flattening in PythonI have been writing a bunch of Python code lately which involves turning
lists of lists into plain lists. As it happens Python does not have
a First, a stupid non-way that is worth writing down for posterity:
This version is a classic mistake (one I made today, in fact). Your
reward for trying this is the cryptic error "
This is both ugly and inefficient, although the inefficiency shouldn't bother you for small lists-of-lists. It is also equivalent to the simpler and slightly faster version:
All addition-based flatteners have the drawback that they only work on actual lists. If you have a list of tuples, or just a list of some sequence type that is not a list, you lose; list addition works only on two lists. The best solution is a rare good use of a multi-level list comprehension:
This will work on any sequence type and for any mixture of sequence types (or iterables); you can have an iterable that returns a mixture of lists, tuples, and other iterables and everything will work out and you will get a list at the end. The multi-level list comprehension approach is both the fastest and the
most general, but it's kind of verbose and hard to follow if you're
not completely up on list comprehensions. I want to like the PS: this is the kind of entry that I write in an attempt to make it
all stick in my head. And in a demonstration that I repeat myself, the
example I used in MultilevelListComps was for list flattening (although
back in 2005 it appears that lists did not have (I mined here and here for this information.) Sidebar: the boring plain approachThe most verbose but straightforward approach is of course the classic
Just as with the list comprehension version, this works with any sequence types or iterable types; it doesn't require lists. On advantage of this approach is that anyone (your future self included) can easily recognize what the code is doing. (4 comments.)
|
These are my WanderingThoughts GettingAround This is part of CSpace, and is written by ChrisSiebenmann. * * * Atom feeds are available; see the bottom of most pages. Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web |