Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Flatten a 2D list

# Python code to demonstrate
# converting 2d list into 1d list
# using list comprehension
 
# import chain
from itertools import chain
 
ini_list = [[1, 2, 3],
            [3, 6, 7],
            [7, 5, 4]]
             
# printing initial list
print ("initial list ", str(ini_list))
 
# converting 2d list into 1d
# using list comprehension
flatten_list = [j for sub in ini_list for j in sub]
 
# printing flatten_list
print ("final_result", str(flatten_list))
Comment

PREVIOUS NEXT
Code Example
Python :: python change terminal name 
Python :: how to pause time in python 
Python :: Scaling Operation in SkLearn 
Python :: how to use virtual environment python 
Python :: create an empty dataframe 
Python :: pyspark join 
Python :: python datetime to seconds 
Python :: Renaming an index in pandas data frame 
Python :: how to get elasticsearch index list using python 
Python :: python continue 
Python :: pandas delete spaces 
Python :: python Non-UTF-8 code starting with 
Python :: plt.imshow not showing image 
Python :: int object is not subscriptable in python 
Python :: The specified file cannot be played on the specified MCI device. The file may be corrupt, not in the correct format, or no file handler available for this format. python 
Python :: print index of tuple python 
Python :: zip django template 
Python :: anova test in python 
Python :: fixed precision float python 
Python :: seaborn bar plot dataset 
Python :: python sentence splitter 
Python :: pandas merge but keep certain columns 
Python :: if found then stop the loop python 
Python :: python get first n elements of dict 
Python :: subsetting based on column value with list 
Python :: how to create a list in python 
Python :: change size of plot python 
Python :: python returen Thread 
Python :: remove extra spaces python 
Python :: median of a list in python 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =