Merge Lists in Python

Merging lists in Python is a common task when working with data collections. Python provides multiple ways to combine lists, including using the + operator, extend() method, list comprehension, and the itertools.chain() function. In this tutorial, we will explore different ways to merge lists with examples.


Examples

1. Merge Lists Using the + Operator

The + operator can merge two or more lists and return a new list.

</>
Copy
# Defining two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Merging lists using the + operator
merged_list = list1 + list2

# Printing the merged list
print("Merged List:", merged_list)

Output:

Merged List: [1, 2, 3, 4, 5, 6]

The + operator creates a new list without modifying the original ones.

2. Merge Lists Using the extend() Method

The extend() method adds elements of one list to another, modifying the existing list.

</>
Copy
# Defining two lists
list1 = ["a", "b", "c"]
list2 = ["d", "e", "f"]

# Merging lists using extend()
list1.extend(list2)

# Printing the merged list
print("Merged List:", list1)

Output:

Merged List: ['a', 'b', 'c', 'd', 'e', 'f']

The extend() method modifies list1 by appending elements of list2.

3. Merge Lists Using List Comprehension

List comprehension provides a concise way to merge lists.

</>
Copy
# Defining two lists
list1 = [10, 20, 30]
list2 = [40, 50, 60]

# Merging lists using list comprehension
merged_list = [item for sublist in [list1, list2] for item in sublist]

# Printing the merged list
print("Merged List:", merged_list)

Output:

Merged List: [10, 20, 30, 40, 50, 60]

This approach is useful when merging lists inside nested structures.

4. Merge Lists Using itertools.chain()

The itertools.chain() function can efficiently merge multiple lists.

</>
Copy
import itertools

# Defining two lists
list1 = [100, 200, 300]
list2 = [400, 500, 600]

# Merging lists using itertools.chain
merged_list = list(itertools.chain(list1, list2))

# Printing the merged list
print("Merged List:", merged_list)

Output:

Merged List: [100, 200, 300, 400, 500, 600]

The itertools.chain() method is efficient when working with large lists as it avoids creating unnecessary intermediate lists.

5. Merge Lists Using the * Operator (Python 3.6+)

Python 3.6+ allows unpacking multiple lists using the * operator.

</>
Copy
# Defining two lists
list1 = ["x", "y"]
list2 = ["z", "w"]

# Merging lists using * operator
merged_list = [*list1, *list2]

# Printing the merged list
print("Merged List:", merged_list)

Output:

Merged List: ['x', 'y', 'z', 'w']

This method provides a readable and efficient way to merge lists.

Conclusion

There are multiple ways to merge lists in Python, each suited for different use cases:

  1. + Operator: Simple, but creates a new list.
  2. extend(): Modifies an existing list by appending another.
  3. List Comprehension: Customizable and useful for nested structures.
  4. itertools.chain(): Efficient for large lists.
  5. * Operator: Modern, concise, and readable (Python 3.6+).