Flatten a List of Lists in Python (original) (raw)
Last Updated : 17 Dec, 2024
Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to **Flatten a list of Lists in Python.
itertools module provides a function called chain which can be used to flatten the list of lists. It is very efficient for large datasets.
Python `
import itertools
Given a list of lists
a = [[1, 2, 3], [4, 5], [6, 7]]
Using itertools.chain to flatten the list
b = list(itertools.chain.from_iterable(a))
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
There are various other ways to Flatten a List of Lists in Python.
Table of Content
- Using numpy.concatenate (for numeric data)
- Using functools.reduce
- Using List Comprehension
- Using sum() Function
- Using For Loop
Using numpy.concatenate
If we are dealing with numeric data, the numpy library provides a very efficient way to flatten lists using concatenate.
Python `
import numpy as np
Given a list of lists
a = [[1, 2, 3], [4, 5], [6, 7]]
Using numpy.concatenate to flatten the list
b = np.concatenate(a).tolist()
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
reduce() function from the functools module can also be used to flatten a list by applying a function that concatenates lists together.
Python `
from functools import reduce
Given a list of lists
a = [[1, 2, 3], [4, 5], [6, 7]]
Using reduce to flatten the list
b = reduce(lambda x, y: x + y, a)
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
Using List Comprehension
List comprehension is a more way to flatten the list. It condenses the same logic of for loop into a single line.
Python `
Given a list of lists
a = [[1, 2, 3], [4, 5], [6, 7]]
Using list comprehension to flatten the list
b = [item for sublist in a for item in sublist]
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
Using sum() Function
sum() function can be used to flatten a list of lists. It works by starting with an empty list and adding each sublist to it.
Python `
a = [[1, 2, 3], [4, 5], [6, 7]]
Using sum to flatten the list
b = sum(a, [])
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
The most basic way to flatten a list of lists is by using a simple for loop. We loop over each sublist inside the main list and append each item to a new list.
Python `
a = [[1, 2, 3], [4, 5], [6, 7]]
Flattening the list using a for loop
b = [] for sublist in a: for item in sublist: b.append(item)
print(b)
`
Output
[1, 2, 3, 4, 5, 6, 7]
Similar Reads
- Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read
- Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python.Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a sin 3 min read
- Create List of Numbers with Given Range - Python The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8 3 min read
- Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples:Add a Single Item Using append()append() method adds one item to the end of the li 3 min read
- How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method:Pythona = [1, 2, 3] a.append(4) prin 2 min read