Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to Flatten a nested list in python

import itertools

a=[[1, 2, 3], [4, 5, 6]]

flat_list = list(itertools.chain.from_iterable(a))
print(flat_list)

# Output:
[1, 2, 3, 4, 5, 6]
Comment

flatten nested list

def flatten(x):
    if isinstance(x, list):
        return [a for i in x for a in flatten(i)]
    else:
        return [x]
Comment

make a nested list flat python

def flatten(t):
    return [item for sublist in t for item in sublist]
Comment

PREVIOUS NEXT
Code Example
Python :: output path jupyter 
Python :: check if a the time is 24 hours older python 
Python :: change font size in plt 
Python :: How to scale a pandas dataframe 
Python :: np.polyfit plot 
Python :: pandas plot several columns 
Python :: how to insert a variable into a string python 
Python :: summary in python 
Python :: python iter on a dic key value 
Python :: python dict append 
Python :: flask error 
Python :: pandas dataframe unique multiple columns 
Python :: shuffle list 
Python :: magic methods python 
Python :: install quick-mailer 
Python :: check pyenv version windows 
Python :: add two list in python 
Python :: return count of substring in a string 
Python :: how to rotate screen with python 
Python :: pandas for column in dataframe 
Python :: play sound on python 
Python :: convert list to nd array 
Python :: how to send file in django response 
Python :: km/h to mph python 
Python :: split data train, test by id python 
Python :: python epoch to datetime 
Python :: generate unique id from given string python 
Python :: how to open pickle file 
Python :: python random list of integers without repetition 
Python :: how to find the data type in python 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =