The task is to convert a nested list into a single list in python i.e no matter how many levels of nesting is there in python list, all the nested has to be removed in order to convert it to a single containing all the values of all the lists inside the outermost brackets but without any brackets inside.
Input : l = [1, 2, [3, 4, [5, 6] ], 7, 8, [9,  ] ]
Output : l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Input : l = [[[‘item1’, ‘item2’]], [[‘item3’, ‘item4’]]]
Output : l = [‘item1’, ‘item2’, ‘itm3, ‘item4”]
We use recursion because the levels of nesting cannot be predetermined.
The original list: [1, 2, [3, 4, [5, 6]], 7, 8, [9, ]] The list after removing nesting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Convert Flat dictionaries to Nested dictionary
- Python - Convert key-values list to flat dictionary
- Python | Check if a nested list is a subset of another nested list
- Python | Convert given list into nested list
- numpy.ndarray.flat() in Python
- Python | Convert list of nested dictionary into Pandas dataframe
- PyQt5 QCommandLinkButton - Setting Flat property
- PyQt5 QCommandLinkButton - Getting Flat property
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert nested sublist into tuples
- Python | Convert string List to Nested Character List
- Python | Split nested list into two lists
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python - Convert Lists to Nested Dictionary
- Python - Convert String to Nested Dictionaries
- Python - Convert Nested Tuple to Custom Key Dictionary
- Python - Convert Nested dictionary to Mapped Tuple
- Convert Python Nested Lists to Multidimensional NumPy Arrays
- Convert nested Python dictionary to object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.