Python> list> Convert double list to single list

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
Python 3.6.0 on virtualenv

Related: Python> tuple> Convert double tuple to single tuple

--Method 1: Comprehension --Method 2: Use itertools

Reference: http://stackoverflow.com/questions/2961983/convert-multi-dimensional-list-to-a-1d-list-in-python Reference: http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python

test_python_170323e.py


import itertools

alist = [[3, 1, 4], [1, 5, 9], [2, 6, 5]]
print(alist)

# 1 
res = [ flatten for inner in alist for flatten in inner ]
print(res)

# 2
res = itertools.chain(*alist)
res = list(res)
print(res)

result


$ python test_python_170323e.py 
[[3, 1, 4], [1, 5, 9], [2, 6, 5]]
[3, 1, 4, 1, 5, 9, 2, 6, 5]
[3, 1, 4, 1, 5, 9, 2, 6, 5]

I personally think that itertools is more readable now (2017/03/23).

Recommended Posts

Python> list> Convert double list to single list
Python> tuple> Convert double tuple to single tuple
Convert list to DataFrame with python
[Python] How to convert a 2D list to a 1D list
python> Convert tuple to list> aList = list (pi_tuple)
Convert strings to character-by-character list format with python
[python] Convert date to string
Convert numpy int64 to python int
[Python] How to use list 1
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
[Introduction to Python] <list> [edit: 2020/02/22]
Convert python 3.x code to python 2.x
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
[Python] How to use list 3 Added
[Python] Convert natural numbers to ordinal numbers
[Python] list
Convert decimal numbers to n-ary numbers [python]
[Introduction to Udemy Python3 + Application] 18. List methods
[Python] How to convert db file to csv
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Django] Convert QuerySet to dictionary type list
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Single pixel camera to experience with Python
[Introduction to Udemy Python3 + Application] 17. List operation
Python amateurs try to summarize the list ①
Convert Python date types to RFC822 format
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Summary of how to use Python list
Convert from Markdown to HTML in Python
[Introduction to Udemy Python3 + Application] 16. List type
Convert absolute URLs to relative URLs in Python
Sample to convert image to Wavelet with Python
Updated to Python 2.7.9
Python basics: list
Convert to HSV
Python> Comprehension / Comprehension> List comprehension
Python list manipulation
"Backport" to python 2
[Python] List Comprehension Various ways to create a list
[python] How to display list elements side by side
Python code to convert region mesh code to latitude / longitude
Convert FBX files to ASCII <-> BINARY in Python
How to clear tuples in a list (Python)
Convert PDF to image (JPEG / PNG) with Python
Convert "number" of excel date to python datetime
Convert PDFs to images in bulk with Python
Python script to convert latitude / longitude to mesh code
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
[Python] Convert from DICOM to PNG or CSV
[Introduction to Udemy Python 3 + Application] 19. Copy of list
Python list comprehensions that are easy to forget
Convert svg file to png / ico with Python