- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to flatten a shallow list in Python?
Flattening a shallow list means converting a nested list into a simple single dimensional list. In other words, converting a multidimensional list into a one-dimensional list.
The process of flattening can be performed by using different techniques like nested for loops, list comprehensions, list concatenation, and by using built-in functions.
In this article, we will discuss a few techniques to flatten a shallow python list.
Using nested for loop
By using nested for loop and list.append() method we can flatten the shallow list. Let’s have a look and see how this can be done in a program.
Example
This simple example successfully flattens a shallow list by using nested for loop. The first loop is for iterating sub lists in list and the second loop is for iterating an item in sublist.
li = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] print("Original shallow list:", li) flatlist = [] for sub_li in li: for ele in sub_li: flatlist.append(ele) print("After flattening :", flatlist)
Output
Original shallow list: [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] After flattening : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
Using a list comprehension
This technique works very similar to the nested for loop, like the above solution is packed in a single line using a list comprehension.
The list comprehension is an easy and elegant way of creating lists based on existing iterable (list, string, tuple).
Syntax
newList = [ expression(element) for element in oldList if condition ]
Example
We flattened a shallow list in a single line of code using a list comprehension method.
li = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] print("Original shallow list:", li) flatlist = [ele for ele in sub_li for sub_li in li] print("List after flattening :", flatlist)
Output
Original shallow list: [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] List after flattening : [90, 90, 90, 100, 100, 100, 110, 110, 110, 120, 120, 120]
Using itertools (chain())
The chain() is a method that accepts a series of iterables and then returns a single iterable which cannot be used directly and has to be explicitly converted. To access the chain() method initially we need to Import itertools module to our program.
Syntax
chain (*iterables)
Example
To unpack the nested list, we have mentioned the *operator.
import itertools li = [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] print("Original shallow list:", li) flatlist = list(itertools.chain(*li)) print("List after flattening :", flatlist)
Output
Original shallow list: [[10, 20, 30, 40], [50, 60, 70, 80], [90, 100, 110, 120]] List after flattening : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
Using sum() function
Summing over inner lists(sub lists) is another solution. Here we will use the python built-in function sum().
Syntax
sum(iterable, start)
Example
In this example, the parameters iterable takes a list of lists and start is an empty list that serves as the initial flat list to which items of the inner sublists are added.
li = [[1, 2, 3, 4], [5, 6, 7], [8, 9]] print("Original shallow list:", li) faltlist = sum(li, []) print("List after flattening :", faltlist)
Output
Original shallow list: [[1, 2, 3, 4], [5, 6, 7], [8, 9]] List after flattening : [1, 2, 3, 4, 5, 6, 7, 8, 9]
We have discussed a few techniques to flatten a shallow python list.