
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Make a Flat List Out of List of Lists in Python
A nested list is a list that has lists as elements. For example: [[1,2,3], [4,5,6], [7,8,9]] is a nested list as it has 3 lists ([1,2,3], [4, 5, 6], and [7, 8, 9]) as its elements.
To flatten a list of lists (nested list), i.e., to convert a 2D list into a 1D list, there are different ways such as Nested for loop, List Comprehension, Built-in Functions, or by importing libraries in Python.
In this article, we will discuss the above methods to flatten a list in Python.
Using for loops
For iterating repeatedly through a sequence, use a for loop (that is either a list, a tuple, a dictionary, a set, or a string). This functions more like an iterator method seen in other object-oriented programming languages and is less like the for keyword found in other programming languages.
Example
In the following example code, we must transform a nested list into a flat list, which is a single 1D list. So, we have used the append() method to add elements to the list.
def flatlist(l): flat_list = [] for element in l: if type(element) is list: for item in element: flat_list.append(item) else: lat_list.append(element) return flat_list lst = [[11, 12, 13], [14, 15, 16, 17], [18, 19]] print('Actual List', lst) print('Flattend List', flatlist(lst))
This will create the following outcome -
Actual List [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattend List [11, 12, 13, 14, 15, 16, 17, 18, 19]
Using List Comprehension
Building new lists from pre-existing ones is made easy by Python's list comprehension functionality. It offers a more concise syntax for building new lists from pre-existing ones and their values.
Example
In this example, the nested list is converted into a single 1D list using the list comprehension method.
original_list = [[11, 12, 13], [14, 15, 16, 17], [18, 19]] flat_list = [item for sublist in original_list for item in sublist] print('Actual List', original_list) print('Flattend list', flat_list)
After running the program, you will get this result -
Actual List [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Flattend list [11, 12, 13, 14, 15, 16, 17, 18, 19]
Using the NumPy library
A general-purpose package for handling arrays is called NumPy. It offers a multidimensional array object with outstanding speed as well as capabilities for interacting with these arrays.
Example
In the following example, we used the NumPy library with other methods in it. Those methods are mentioned below.
concatenate() function is used to concatenate a sequence of arrays.
flat() function is used as a 1D iterator over an N Dimensional array.
import numpy originallist = [[11, 12, 13], [14, 15, 16, 17], [18, 19]] flatlist = list(numpy.concatenate(originallist).flat) print('Actual List', originallist) print('Transformed list', flatlist)
This output will be displayed when the program runs -
Actual List [[11, 12, 13], [14, 15, 16, 17], [18, 19]] Transformed list [11, 12, 13, 14, 15, 16, 17, 18, 19]