Open In App

Merge Multiple Lists into one List

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Using itertools.chain()

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

Next Article
Practice Tags :

Similar Reads