👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
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
statement:
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 QuerySet
object.
Conclusion:
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 itertools.chain
:
>>> 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],[3],[5,89],[],[6]])
>>> print(list(chain))
>>> [1, 2, 3, 5, 89, 6]
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Flattening a shallow list in Python, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Flattening a shallow list in Python
- Deutsch Flattening a shallow list in Python
- Français Flattening a shallow list in Python
- Español Flattening a shallow list in Python
- Türk Flattening a shallow list in Python
- Русский Flattening a shallow list in Python
- Português Flattening a shallow list in Python
- Polski Flattening a shallow list in Python
- Nederlandse Flattening a shallow list in Python
- 中文 Flattening a shallow list in Python
- 한국어 Flattening a shallow list in Python
- 日本語 Flattening a shallow list in Python
- हिन्दी Flattening a shallow list in Python
Prague | 2023-02-06
Thanks for explaining! I was stuck with Flattening a shallow list in Python for some hours, finally got it done 🤗. Checked yesterday, it works!
Warsaw | 2023-02-06
io Python module is always a bit confusing 😭 Flattening a shallow list in Python is not the only problem I encountered. Checked yesterday, it works!
Berlin | 2023-02-06
Simply put and clear. Thank you for sharing. Flattening a shallow list in Python and other issues with code Python module was always my weak point 😁. I just hope that will not emerge anymore