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 :: flatten a 2d list 
Python :: python dictionary comprehension 
Python :: how to make a sigmoid function in python 
Python :: how to redirect in django rest framework 
Python :: python calculate angle between two points 
Python :: append one row to pandas dataframe 
Python :: set size of button tkinter 
Python :: print only numbers from string python 
Python :: load a Dictionary from File in Python Using the Load Function of the pickle Module 
Python :: pep full form 
Python :: user group template tag django 
Python :: pandas to latex 
Python :: python date iso 8601 
Python :: mutable and immutable in python 
Python :: get one from dataloader 
Python :: python manage.py collectstatic --noinput 
Python :: mac catallina python3 
Python :: plotting two columns of a dataframe in python 
Python :: read specific rows from csv in python 
Python :: django template for loop 
Python :: replace value in dataframe 
Python :: round up division python 
Python :: dataframe drop rows by column value 
Python :: assign python 
Python :: read excel file in python 
Python :: install python 3.6 on centos 
Python :: python get date from unix timestamp 
Python :: how to create a tuple from csv python 
Python :: python pillow resize image 
Python :: python how to import library absoluth path 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =