In Python, list data structures are utilized to store the collection of data with different data types, unlike arrays. Sometimes to store large data we need to concatenate more than one list in Python. To concatenate multiple lists various methods are used such as the “+” operator, “extend()” function, list comprehension, etc.
In this tutorial, we explain the below-mentioned methods to concatenate two lists in Python:
- Method 1: Using the “+” Operator
- Method 2: Using extend() Function
- Method 3: Using itertools.chain()
- Method 4: Using the ‘*’ Operator
- Method 5: Using List Comprehension
Method 1: Using the “+” Operator
The concatenation operator “+” is utilized to join/concatenate two or more than two lists in Python. Let’s see various examples of how to add two lists in Python using this method:
Example 1: Concatenate Two Lists
The below code uses the concatenation operator “+” to add two lists:
Code:
list1 = [45, 25, 30, 15]
list2 = ['A', 'B', 'C', 'D']
output = list1 + list2
print(output)
Initially, two lists named “list1” and “list2” are initialized. Next, the concatenation operator “+” is used to add the given two lists and store the result in the “output” variable.
Output:
The input two lists have been added.
Example 2: Concatenate More Than Two Lists
The “+” operator is capable of adding multiple lists in Python. Here is an example:
Code:
list1 = [45, 25, 30, 15]
list2 = ['A', 'B', 'C', 'D']
list3 = [55, 66, 77, 88]
list4 = ['X', 'Y', 'Z', 'Q']
output = list1 + list2 + list3 + list4
print(output)
Multiple lists named “list1”, “list2”, “list3” and “list4” are initialized in the program. Next, the “+” operator is used between these lists for concatenation.
Output:
The above snippet shows that the given lists have been concatenated successfully.
Method 2: Using extend() Function
The “extend()” function is used to concatenate the specified list items or an iterable to the end of the list. Let’s understand it by the following examples:
Example 1: Concatenate Two Lists
The below code uses the “extend()” function to concatenate two lists:
Code:
list1 = [45, 25, 30, 15]
list2 = ['A', 'B', 'C', 'D']
list1.extend(list2)
print(list1)
The “list1.extend()” function accepts the list value named “list2” and adds it to the end of another list named “list1”.
Output:
The list named “list1” has been extended.
Method 3: Using itertools.chain()
The “itertools.chain()” function accepts various iterable values as a parameter and retrieves a new iterable. The new iterable is a group of all input iterable as a single iterable value. The below code uses the “itertools.chain()” function to concatenate two lists in Python:
Code:
import itertools
list_a = [45, 58, 36, 98, 15]
list_b = [5.5, 45, 25, 33, 3.3]
output = list(itertools.chain(list_a, list_b))
print (output)
- First, the “itertools” module is imported and the list values are initialized in the program.
- The “itertools.chain()” function takes the input list as an argument and returns a new iterator that contains the elements of input lists.
- The returned iterator is passed inside the “list()” function to convert into a list.
Output:
Two lists have been concatenated in the above output.
Method 4: Using the ‘*’ operator
In Python, the “*” operator is utilized to concatenate two Python lists:
Code:
list_a = ['Joseph', 'Henry']
list_b = [5.5, 45, 25, 33, 3.3]
output = [*list_a, *list_b]
print (output)
In the above code:
- The two lists named “list_a” and “list_b” are initialized in the program.
- The expression“*list_a” and “*list_b” is used to unpack the list and return the elements of the list.
- The expression “[*list_a, *list_b]” will concatenate the two lists and return the new list that contains the elements of “list_a” followed by elements of “list_b”.
Output:
The two given lists have been concatenated.
Method 5: Using List Comprehension
The following code uses the list comprehension method to concatenate two lists:
Code:
list_a = ['Joseph', 'Henry']
list_b = [5.5, 45, 25, 33, 3.3]
output = [i for items in [list_a, list_b] for i in items]
print(output)
- The nested for loop is used in the list comprehension to concatenate the given list.
- In list comprehension, we iterate over a new list named “[list_a, list_b]”.
- For each element in this list, we are iterating over the items in the inner list
- For each item in the inner list, we are appending it to the output list.
Output:
The above snippet shows that the given two lists have been concatenated.
Conclusion
To concatenate two lists the “+ Operator”, “extend()” function, “itertools.chain()” function, “the ‘*’ operator” and list comprehension is used in Python. The most straightforward and easy way to add two or more lists is by using the concatenation operator “+”. This guide demonstrates various ways to concatenate two Python lists.