Articles

python program to flatten a nested list

python program to flatten a nested list


In the python programming article, we are going to learn:

  • python program to flatten a nested list

Program to flatten a nested list in Python

The python program to flatten a nested list is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

import pandas as pd
nested_list = [22,11,[34,[56,78]],61,[35,91,77,59]]

print("The new list:",list(pd.core.common.flatten(nested_list)))
        

The output of the python program to flatten a nested list is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The new list: [22, 11, 34, 56, 78, 61, 35, 91, 77

Few important tips about the program

1. To run this program, we will need to have pandas library installed into our system. If it is not, in the terminal type:

"pip install pandas" and wait.

2. The flatten() method in the pandas.core.common package flattens a list, irrespective of its dimensionality.

 

python program to flatten a nested list snapshot

flatten a list in python

This wraps up our session on python program to flatten a nested list.


Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!