Open In App

Merge Two Lists in Python

Last Updated : 15 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python provides several approaches to merge two lists. In this article, we will explore different methods to merge lists with their use cases. The simplest way to merge two lists is by using the + operator.

Let’s take an example to merge two lists using + operator.

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Merge the two lists and assign
# the result to a new list
c = a + b
print(c)

Output
[1, 2, 3, 4, 5, 6]

Explanation: The + operator creates a new list by concatenating a and b. This operation does not modify the original lists but returns a new combined list.

Lets see other different methods to merge two lists:

Using the extend()

Another common method is to use the extend() function, which modifies the original list by adding elements from another list.

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Add all elements from list 'b' to the end of list 'a'
a.extend(b)

print(a)

Output
[1, 2, 3, 4, 5, 6]

Explanation: The extend() is an in-place operation that appends each element of list b to list a. This means that the original list a gets updated without creating a new list, which makes it more memory efficient for large lists.

Using the * Operator

We can use the * operator to unpack the elements of multiple lists and combine them into a new list.

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Use the unpacking operator to merge the lists
c = [*a, *b]

print(c)

Output
[1, 2, 3, 4, 5, 6]

Explanation: The * operator unpacks the elements of a and b, placing them directly into the new list c.

Using for loop

We can also merge two lists using a simple for loop. This approach is provides more control if we need to perform additional operations on the elements while merging.

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Initialize an empty list to store the merged elements
res = []

# Append all elements from the first list
for val in a:
    res.append(val)

# Append all elements from the second list
for val in b:
    res.append(val)

print(res)

Output
[1, 2, 3, 4, 5, 6]

Explanation:

  • for item in a: Iterates over each element in list a and appends it to list res.
  • for item in b: Iterates over each element in list b and appends it to list res.

Using list comprehension

List comprehension is an efficient and concise alternative to the for loop for merging lists. It allows us to iterate through each list and merge them into a new one in a single line of code.

Python
a = [1, 2, 3]
b = [4, 5, 6]

# Use list comprehension to create a new merged list
c = [item for item in a] + [item for item in b]

print(c)

Output
[1, 2, 3, 4, 5, 6]

Explanation: List comprehension iterates over both lists a and b, creating a new list by adding all items from each list.

Using the itertools.chain()

The itertools.chain() method from the itertools module provides an efficient way to merge multiple lists. It doesn’t create a new list but returns an iterator, which saves memory, especially with large lists.

Python
# imports chain function from itertools module
from itertools import chain

a = [1, 2, 3]
b = [4, 5, 6]

# Use itertools.chain to merge the lists in an efficient manner
c = list(chain(a, b))

print(c)

Output
[1, 2, 3, 4, 5, 6]

Explanation: list(chain(a, b)) combines the lists a and b. The chain(a, b) creates an iterator that collect all items from a first then all items from b. Then, change this iterator into a list will gives us merged list.

Which method to choose?

  • + Operator: Quick and simple for small lists but can consume a lot of memory for larger ones.
  • extend() Method: Efficiently merges large lists by modifying the original list in place.
  • Unpacking (*): It provides a clean syntax, however, it may not be optimal for very large lists due to memory usage.
  • itertools.chain(): It works best for large lists. It merges without creating additional memory.
  • For Loop: It provides more control to use complex merging conditions.
  • List Comprehension: This is an alternative approach of using “for loop”, it has concise and cleaner syntax.




Next Article
Practice Tags :

Similar Reads