If you have a nested list (list of lists) in Python and you want to convert it into a single list i.e. flatten a nested list, you can try writing a recursion function.
Example 1: Using recursion function:
def flattern_nested_list(input_nested_list):
flat_list = []
for element in input_nested_list:
if isinstance(element, list):
flat_list.extend(flattern_nested_list(element)) # recursion
else:
flat_list.append(element)
return flat_list
# nested list
my_nested_list = [1,2,3,4,[5,6,7,8],[[9,10],11,12]]
# calling our custom function to get flattern list
my_flat_list = flattern_nested_list(my_nested_list)
# print the result
print(my_flat_list)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Example 2: Using List Comprehension:
my_nested_list = [
["USA", "Canada", "Mexico"],
["India", "China","Japan"],
["Australia", "Sweden", "France", "UK"]
]
flat_list = [item for sub_list in my_nested_list for item in sub_list]
print(flat_list)
Output:
['USA', 'Canada', 'Mexico', 'India', 'China', 'Japan', 'Australia', 'Sweden', 'France', 'UK']
In the above example flat_list is created using a list comprehension with nested loops. The outer loop iterates through each sub_list in the nested list, and the inner loop iterates through each item in the sub_list, appending it to the flat_list.
References:
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensionsHave Questions? Post them here!
- tkinter - Hello World! Program
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew
- How to install SpaCy (NLP Library) on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- How to uninstall pip Python packages
- 3 Ways to find if element is present in a List in Python
- How to Convert Python String to DateTime Object
- Python f-strings Formatted String Literals Syntax and Examples
- Where does brew install python in macOS
- Take input argument from command line in Python Programming
- Advanced print() Function Tutorial and Techniques for Python Developers
- How to add borders to tkinter label text
- Whats new in Python 3.10 Pre-release
- Float built-in function in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- How to check if Key Exists in Python Dictionary?
- Read a file line by line in Python Program
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- What is the Max and Minimum Value of int type in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- Fix: TypeError: can only concatenate str (not int) to str in Python
- How to take user input from the console in a Python program
- [Fix] TypeError: str object is not callable in Python
- 3 Python program to add two numbers
- How to delete a file using Python code example
- Disable Fading Edges Scroll Effect Android Views - Android
- List of 32 CSS cursors web developers must know about - CSS
- How to hide or display Wifi icon in macOS Bug Sur Menu Bar - MacOS
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated - Java
- [Fix] zsh: command not found: python on Mac - Python
- Google Celebrates Rosa Bonheur 200th birthday with a Google Doodle - Google
- How to install Android Studio Chipmunk and SDK tools on macOS (2021.2) - Android-Studio
- Android Disable EditText from Auto Focus on Activity load - Android