How do I flatten a two-dimensional list in python?
ExampleCopy code to clipboard
>>> list = [[1, 2], [3, 4]] >>> [item for child in list for item in child]
[1, 2, 3, 4]
List comprehension to the rescue! I was looking for a LINQ SelectMany function in python, only to discover this can be done with list comprehension without the use for libraries like numpy.
Link to documentation or other source
14 Oct 2019
Author: Loek van den Ouweland
Would you recommend this example to your colleagues?