Merge Multiple Lists into one List
Last Updated :
19 Dec, 2024
In this article, we are going to learn how to merge multiple lists into one list.
extend()
method is another simple way to merge lists in Python. It modifies the original list by appending the elements of another list to it. This method does not create a new list but instead adds elements to an existing one.
Python
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# Extending list a by appending elements of list b to it
a.extend(b)
# Extending list a again by appending elements of list c to it
a.extend(c)
print(a)
Output[1, 2, 3, 4, 5, 6, 7, 8, 9]
Let's explore some more methods to merge multiple lists into one list.
Using '+' Operator
The simplest and most common way to merge lists in Python is using the +
operator. This method concatenates two or more lists into a single list.
Python
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
#using the '+' operator to create a new list
merged_list = a + b + c
print(merged_list)
Output[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
- The '
+'
operator joins two or more lists into a new list. - This method is simple and efficient for merging a small number of lists.
- The original lists remain unchanged, and the new list contains all elements in the order they appear in the individual lists.
itertools.chain()
function is part of Python's itertools
module and provides a memory-efficient way to merge multiple lists. It iterates over the given lists and produces a flattened version of all elements, without needing to create an intermediate list.
Python
import itertools
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# Using itertools.chain to merge the lists into a single iterable
m = list(itertools.chain(a, b, c))
print(m)
Output[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:
itertools.chain()
takes any number of iterable arguments and produces a single iterable, which we convert into a list using list()
.- This method is efficient because it processes the elements one at a time without storing them all in memory.
Using List Comprehension
List comprehension allows you to merge multiple lists and optionally filter or modify the elements as you combine them. This method is flexible and can be adapted for various scenarios.
Python
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
# flatten the list of lists [a, b, c]
m = [item for sublist in [a, b, c] for item in sublist]
print(m)
Output[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation
- The outer loop iterates over each sublist, and the inner loop extracts each element from the sublist.
- This method is useful when we need to perform operations on the elements while merging them, such as filtering or transforming the elements.
Using for Loop
A simple for
loop can also be used to merge multiple lists by iterating over each list and appending its elements to a new list.
Python
a = [1, 2, 3]
b = [4, 5, 6]
c = [7, 8, 9]
merged_list = []
for lst in [a, b, c]:
merged_list += lst
print(merged_list)
Output[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation
for
loop iterates over each list in a list of lists, and the +=
operator appends each list’s elements to the merged_list
Similar Reads
Merge Two Lists into List of Tuples - Python The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
3 min read
Python | Initializing multiple lists In real applications, we often have to work with multiple lists, and initialize them with empty lists hampers the readability of code. Hence a one-liner is required to perform this task in short so as to give a clear idea of the type and number of lists declared to be used. Method #1: Using loops We
4 min read
How To Combine Multiple Lists Into One List Python Combining multiple lists into a single list is a common operation in Python, and there are various approaches to achieve this task. In this article, we will see how to combine multiple lists into one list in Python. Combine Multiple Lists Into One List PythonBelow are some of the ways by which we ca
2 min read
Python - Append Multiple Lists at Once Our task is to merge multiple lists into a single list by appending all their elements. For example, if the input is a = [1, 2], b = [3, 4], and c = [5, 6], the output should be [1, 2, 3, 4, 5, 6].Using itertools.chain()itertools.chain() function from the itertools module is another way to combine l
4 min read
Python - List of tuples to multiple lists Converting a list of tuples into multiple lists involves separating the tuple elements into individual lists. This can be achieved using methods like zip(), list comprehensions or loops, each offering a simple and efficient way to extract and organize the data.Using zip()zip() function is a concise
3 min read