Is there a simple way to flatten a list of iterables with a list comprehension, or failing that, what would you all consider to be the best way to flatten a shallow list like this, balancing performance and readability?
I tried to flatten such a list with a nested list comprehension, like this:
[image for image in menuitem for menuitem in list_of_menuitems]
But I get in trouble of the
NameError variety there, because the
name "menuitem" is not defined. After googling and looking around on Stack Overflow, I got the desired results with a
reduce(list.__add__, map(lambda x: list(x), list_of_menuitems))
But this method is fairly unreadable because I need that
list(x) call there because x is a Django
Thanks to everyone who contributed to this question. Here is a summary of what I learned. I"m also making this a community wiki in case others want to add to or correct these observations.
My original reduce statement is redundant and is better written this way:
>>> reduce(list.__add__, (list(mi) for mi in list_of_menuitems))
This is the correct syntax for a nested list comprehension (Brilliant summary dF!):
>>> [image for mi in list_of_menuitems for image in mi]
But neither of these methods are as efficient as using
>>> from itertools import chain >>> list(chain(*list_of_menuitems))
And as @cdleary notes, it"s probably better style to avoid * operator magic by using
chain.from_iterable like so:
>>> chain = itertools.chain.from_iterable([[1,2],,[5,89],,]) >>> print(list(chain)) >>> [1, 2, 3, 5, 89, 6]
If you"re just looking to iterate over a flattened version of the data structure and don"t need an indexable sequence, consider itertools.chain and company.
>>> list_of_menuitems = [["image00", "image01"], ["image10"], ] >>> import itertools >>> chain = itertools.chain(*list_of_menuitems) >>> print(list(chain)) ["image00", "image01", "image10"]
It will work on anything that"s iterable, which should include Django"s iterable
QuerySets, which it appears that you"re using in the question.
Edit: This is probably as good as a reduce anyway, because reduce will have the same overhead copying the items into the list that"s being extended.
chain will only incur this (same) overhead if you run
list(chain) at the end.
Meta-Edit: Actually, it"s less overhead than the question"s proposed solution, because you throw away the temporary lists you create when you extend the original with the temporary.
You almost have it! The way to do nested list comprehensions is to put the
for statements in the same order as they would go in regular nested
for inner_list in outer_list: for item in inner_list: ...
[... for inner_list in outer_list for item in inner_list]
So you want
[image for menuitem in list_of_menuitems for image in menuitem]
This first edition of Strategic Engineering for Cloud Computing and Big Data Analytics focuses on addressing numerous and complex, inter-related issues which are inherently linked to systems engineeri...
Data scientist has been called “the sexiest job of the 21st century,” presumably by someone who has never visited a fire station. Nonetheless, 23/09/2020
The ability to identify patterns is an essential component of sensory intelli- gent machines. Pattern recognition is therefore an indispensible component of the so-called “Intelligent Control System...
R for Everyone: Advanced Analytics and Graphics. ...