How to Concatenate Two Lists in Python

Concatenation refers to combining two or more lists into a single list. In Python, there are several ways to concatenate lists, including the + operator, extend() method, list comprehension, and itertools.chain(). In this tutorial, we will explore these methods with examples.


Examples

1. Concatenate Two Lists Using the + Operator

The simplest way to concatenate two lists in Python is by using the + operator. This creates a new list containing elements from both lists.

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

# Concatenating using the + operator
result = list1 + list2

# Printing the concatenated list
print("Concatenated List:", result)

Output:

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

The + operator creates a new list instead of modifying the existing ones.

2. Concatenate Two Lists Using the extend() Method

The extend() method modifies an existing list by adding elements from another iterable.

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

# Concatenating using extend()
list1.extend(list2)

# Printing the updated list1
print("Concatenated List:", list1)

Output:

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

Unlike the + operator, extend() modifies list1 in place rather than creating a new list.

3. Concatenate Two Lists Using List Comprehension

List comprehension provides a concise way to merge two lists.

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

# Concatenating using list comprehension
result = [item for item in list1] + [item for item in list2]

# Printing the concatenated list
print("Concatenated List:", result)

Output:

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

List comprehension allows additional transformations while merging lists.

4. Concatenate Two Lists Using itertools.chain()

The itertools.chain() function efficiently concatenates multiple iterables.

</>
Copy
import itertools

# Defining two lists
list1 = ["x", "y"]
list2 = ["z", "w"]

# Concatenating using itertools.chain()
result = list(itertools.chain(list1, list2))

# Printing the concatenated list
print("Concatenated List:", result)

Output:

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

itertools.chain() is memory-efficient, especially for large lists, as it avoids creating intermediate lists.

5. Using the * Operator (Python 3.6+)

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

In the following program, we unpack the elements of list1 and list2 to a new list result.

</>
Copy
# Defining two lists
list1 = [100, 200]
list2 = [300, 400]

# Concatenating using * operator
result = [*list1, *list2]

# Printing the concatenated list
print("Concatenated List:", result)

Output:

Concatenated List: [100, 200, 300, 400]

The * operator provides a clean and efficient way to concatenate lists.

Conclusion

Python provides multiple ways to concatenate lists:

  1. + Operator: Creates a new concatenated list.
  2. extend() Method: Modifies an existing list.
  3. List Comprehension: Allows additional transformations.
  4. itertools.chain(): Efficient for large lists.
  5. * Operator: Concise and modern approach (Python 3.6+).