Python: Flatten a given nested list structure
Flatten Nested List Structure
Write a Python program to flatten a given nested list structure.
Sample Solution-1:
Python Code:
Sample Output:
Original list: [0, 10, [20, 30], 40, 50, [60, 70, 80], [90, 100, 110, 120]] Flatten list: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
Flowchart:

Sample Solution-2:
Loop over elements, use list.extend() if the element is a list, list.append() otherwise.
Python Code:
Sample Output:
Original list: [0, 10, [20, 30], 40, 50, [60, 70, 80], [90, 100, 110, 120]] Flatten list: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to flatten a list containing nested lists and tuples.
- Write a Python program to flatten a nested list up to a specified depth.
- Write a Python program to flatten a list while preserving non-list elements as-is.
- Write a Python program to convert a nested list into a single-level dictionary.
Go to:
Previous: Write a Python program to check whether all dictionaries in a list are empty or not.
Next: Write a Python program to remove consecutive (following each other continuously) duplicates (elements) of a given list.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.