Merge Multiple Lists into One in Python

To merge multiple lists into one in Python, you can use methods like the + operator, extend() method, list comprehension, itertools.chain(), and the sum() function. Each method provides a unique way to concatenate lists based on your specific needs.


Examples

1. Merging Lists Using the + Operator

The + operator allows concatenation of multiple lists by creating a new merged list.

</>
Copy
# Define multiple lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]

# Merge lists using the + operator
merged_list = list1 + list2 + list3

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

Here, list1, list2, and list3 are combined using the + operator, creating a new list merged_list that contains all the elements in sequential order.

Output:

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

2. Merging Lists Using extend() Method

The extend() method modifies the original list by appending elements from another iterable.

</>
Copy
# Define multiple lists
list1 = ["a", "b", "c"]
list2 = ["d", "e", "f"]
list3 = ["g", "h", "i"]

# Merge lists using extend()
list1.extend(list2)
list1.extend(list3)

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

Here, the extend() method adds elements of list2 and list3 to list1. Unlike the + operator, it modifies the original list instead of creating a new one.

Output:

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

3. Merging Lists Using List Comprehension

List comprehension allows merging multiple lists into one in a single expression.

</>
Copy
# Define multiple lists
list1 = [10, 20]
list2 = [30, 40]
list3 = [50, 60]

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

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

Here, we use nested list comprehension. The outer loop iterates over each sublist (list1, list2, list3), and the inner loop extracts elements from each sublist to create a flat list.

Output:

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

4. Merging Lists Using itertools.chain()

The itertools.chain() function from the itertools module provides an efficient way to merge multiple lists.

</>
Copy
import itertools

# Define multiple lists
list1 = [100, 200]
list2 = [300, 400]
list3 = [500, 600]

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

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

Here, itertools.chain() takes multiple iterables and returns a single iterable, which we convert into a list. This approach is efficient for handling large datasets.

Output:

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

5. Merging Lists Using sum() Function

The sum() function can be used with an empty list as the initial value to merge multiple lists.

</>
Copy
# Define multiple lists
list1 = [5, 10]
list2 = [15, 20]
list3 = [25, 30]

# Merge lists using sum()
merged_list = sum([list1, list2, list3], [])

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

Here, sum() iterates over the list of lists and concatenates them starting from an empty list []. However, this method is less efficient than itertools.chain() due to repeated list copying.

Output:

Merged List: [5, 10, 15, 20, 25, 30]

Conclusion

Python provides multiple ways to merge lists:

  1. + Operator: Creates a new merged list.
  2. extend(): Modifies the original list.
  3. List Comprehension: Offers a concise and flexible way to merge.
  4. itertools.chain(): Efficient for large datasets.
  5. sum() Function: Less efficient but works for merging.