Concatenate Lists in Python

There are several ways to concatenate (or join) lists in Python. Here are the 5 most common methods:

(1) Using the + operator

This is the most straightforward method to concatenate lists in Python. The + operator creates a new list by appending the second list to the end of the first list.

For example:

list1 = [5, 10, 15]
list2 = [20, 25, 30]

concatenated_list = list1 + list2
print(concatenated_list) # Output: [5, 10, 15, 20, 25, 30]

(2) Using the extend() method

The extend() method modifies the original list by appending the elements of another iterable (like a list) to the end.

For example:

list1 = [5, 10, 15]
list2 = [20, 25, 30]

list1.extend(list2)
print(list1) # Output: [5, 10, 15, 20, 25, 30]

This method modifies the original list.

(3) Using list comprehension

List comprehension provides a concise way to create a new list by iterating over multiple lists.

For example:

list1 = [5, 10, 15]
list2 = [20, 25, 30]

concatenated_list = [item for sublist in [list1, list2] for item in sublist]
print(concatenated_list) # Output: [5, 10, 15, 20, 25, 30]

In the above code:

(1) item for sublists in [list1, list2]...] : This part iterates through the list [list1, list2], which contains two input lists. In each iteration, sublist will be assigned to either list1 or list2.

(2) ...for item in sublist : This nested loop iterates through each element (item) within the current sublist.

(3) item : This is the expression that determines the value for each element in the resulting concatenated list.

List comprehension is typically less readable than using the + operator or extend() method for simple concatenation. However, it is particularly useful when you need to combine concatenation with transformations, filtering, or processing multiple lists.

list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Concatenate the lists and squares each element using list comprehension
concatenated_list = [item **2 for sublist in [list1, list2] for item in sublist]
print(result)  # Output: [1, 4, 9, 16, 25, 36]

(4) Using the unpacking operator

The unpacking operator * is used to unpack elements of a list. However, it can also be used to concatenate multiple lists.

For example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

concatenated_list = [*list1, *list2]
print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6]

(5) Using the itertools.chain()

The itertools.chain() method is used to concatenate multiple iterables (like lists, tuples, or other iterable objects) into a single iterable.

It returns an iterator that goes through each element of the input iterables one by one, without creating a new list in memory. This makes it memory efficient for working with large datasets.

For example:

import itertools 

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = [7, 8, 9]

concatenated_list = list(itertools.chain(list1, list2, list3))
print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

The list() function converts the iterable object into a list.

Choosing the right method

Use the + operator if you need a new list by concatenating two or more lists.

Use the extend() method if you need to modify the original list by adding elements from another iterable (list or tuple).

Use list comprehension if you need to concatenate and apply transformations or filters to the elements.

Use the unpacking operator if you want a concise and readable way to concatenate multiple lists.

Use the itertools.chain() if you need memory efficiency when concatenating large datasets or multiple iterables.