Draft for Information Only
Content
Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Flatten a List of Lists Sources and References
Flatten a List of Lists
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> [item for sublist in L for item in sublist]
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> import itertools
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> list(itertools.chain(*L))
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> import itertools
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> list(itertools.chain.from_iterable(L))
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> sum(L, [])
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> from functools import reduce
>>> import operator
>>> reduce(operator.concat, L)
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> from functools import reduce
>>> import operator
>>> reduce(operator.iconcat, L, [])
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> X=[]
>>> [X.extend(each) for each in L]
[None, None, None]
>>> X
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> import numpy as np
>>> list(np.array(L).flat)
<stdin>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested seq
uences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different l
engths or shapes) is deprecated. If you meant to do this, you must specify 'dtyp
e=object' when creating the ndarray.
[1, 2, 3, 4, 5, 6, [7, 8], [9, 0]]
>>>
Flatten a List of Lists
Flatten a List of Lists Python Code Input:Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AM
D64)] on win32
>>> L=[[1,2,3],[4,5,6],[[7,8],[9,0]]]
>>> import numpy as np
>>> list(np.concatenate(L))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the ar
ray at index 0 has 1 dimension(s) and the array at index 2 has 2 dimension(s)
>>>
Sources and References
https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists
©sideway
ID: 211100030 Last Updated: 11/30/2021 Revision: 0
|
 |