[Python] How to convert a 2D list to a 1D list

Sometimes you want to convert a 2D list to a 1D list in Python. It can be converted in one line by using the sum function.

If you don't know it, you won't know what you're doing at first glance, but it's surprisingly convenient to use. If you want to convert a 3D list to a 1D list, you can use the sum function twice.

x = [[1, 2], [3, 4]]
x = sum(x, [])
print(x) # [1, 2, 3, 4]

y = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
y = sum(sum(y, []), [])
print(y) # [1, 2, 3, 4, 5, 6, 7, 8]

Use itertools.chain.from_iterable () for speed. This is faster than using the sum function.

import itertools
x = [[1, 2], [3, 4]]
x = itertools.chain.from_iterable(x)
print(list(x)) # [1, 2, 3, 4]

y = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
y = itertools.chain.from_iterable(list(itertools.chain.from_iterable(y)))
print(list(y)) # [1, 2, 3, 4, 5, 6, 7, 8]

In addition to the above, you can also create your own function. It does not depend on the number of dimensions in the list, so it is recommended when handling data with various dimensions.

https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists

Recommended Posts

[Python] How to convert a 2D list to a 1D list
How to clear tuples in a list (Python)
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
How to convert / restore a string with [] in python
How to write a list / dictionary type of Python3
[Python] How to create a 2D histogram with Matplotlib
How to write a Python class
Convert list to DataFrame with python
Python> list> Convert double list to single list
[Python] How to use list 3 Added
[Python] How to make a list of character strings character by character
How to convert an array to a dictionary with Python [Application]
How to shuffle a part of a Python list (at random.shuffle)
How to transpose a 2D array using only python [Note]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Python] How to convert db file to csv
[Python] How to make a class iterable
How to convert Python to an exe file
[Python] How to invert a character string
How to get a stacktrace in python
python> Convert tuple to list> aList = list (pi_tuple)
Summary of how to use Python list
How to run a Maya Python script
How to remove duplicates from a Python list while preserving order.
How to delete multiple specified positions (indexes) in a Python list
How to install Python
How to install python
How to use list []
[Python] List Comprehension Various ways to create a list
[python] How to display list elements side by side
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to open a web browser from python
How to embed a variable in a python string
How to draw a 3D graph before optimization
How to create a JSON file in Python
How to generate a Python object from JSON
How to add a Python module search path
How to convert SVG to PDF and PNG [Python]
Convert a multidimensional list (array) to one dimension
[Algorithm x Python] How to use the list
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
A python amateur tries to summarize the list ②
Convert strings to character-by-character list format with python
How to remove duplicate elements in Python3 list
[Python] How to extract / delete / convert a matrix containing missing values (NaN)
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to format a list of dictionaries (or instances) well in Python
[Python --open3d] How to convert obj data of 3D model to point cloud
How to pass the execution result of a shell command in a list in Python
[Python] How to remove duplicate values from the list
[2020.8 latest] How to install Python
[python] Convert date to string
Convert numpy int64 to python int
Convert a slice object to a list of index numbers
python3: How to use bottle (2)