w3resource
Python Exercises

Python: Flatten a shallow list

Python List: Exercise - 23 with Solution

Write a Python program to flatten a shallow list.

Example - 1 :

Python: Flatten a shallow list

Example - 2 :

Python: Flatten a shallow list

Example - 3 :

Python: Flatten a shallow list

Sample Solution:-

Python Code:

import itertools
original_list = [[2,4,3],[1,5,6], [9], [7,9,0]]
new_merged_list = list(itertools.chain(*original_list))
print(new_merged_list)

Sample Output:

[2, 4, 3, 1, 5, 6, 9, 7, 9, 0] 

Flowchart:

Flowchart: Flatten a shallow list

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the index of an item in a specified list.
Next: Write a Python program to append a list to the second list.

What is the difficulty level of this exercise?