How to Concatenate two or multiple Lists in Python
In this article, we will learn to concatenate two or multiple lists together in Python. We will use some built-in functions and some custom codes as well. Let's first have a quick look over what is a list and then how concatenation of lists takes place in Python.
Python Lists
Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the lists can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable means mutable, whose value can be changed upon creation and allow duplicate values. For example,
Python List Example
list1 = [‘Ram’,’Arun’,‘Kiran’]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]
Concatenation means adding two or more values together as a single entity in an end-to-end manner. This operation is useful when we have a number of lists of elements that need to be processed in a similar manner. Concatenation may result in an original modified list or prints the new modified list. Let's see some useful ways to concatenate a list.
List Concatenation Using Plus(+) Operator
This operator is similar to adding a property. It joins a different set of values together. This the easiest approach of concatenation. In this, two or more lists are defined to concatenate, and then the result is stored in a new variable. It appends values in the order the same as given in the original list.
Example: Concatenation Two Lists
It joins two lists. It can easily add the whole list2 behind the other list1 and hence perform the concatenation.
list1 = ["x", "y" , "z"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
['x', 'y', 'z', 1, 2, 3]
Example: Concatenation More Lists
It joins two or more lists. Just like the above two lists, list1, list2, list3 will be added one after another.
list1 = ["x", "y" , "z"]
list2 = [1, 2, 3]
list3 = ["bat", "cat", "rat"]
list4 = list1 + list2 + list3
print(list4)
['x', 'y', 'z', 1, 2, 3, ‘bat’ , ‘cat’ , ‘rat’]
List Concatenation Using append() Function
By using the append()
function, the program will join two lists by appending the values from one list to another list. During this method, we traverse the second list and keep appending elements to the first list, in order that the primary list now has all the elements of both the lists and hence would perform the append.
Example: Concatenation Two Lists
The for loop will iterate over each element of list2 and will append that element one by one to list1.
list1 = ["x", "y" , "z"]
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
print(list1)
['x', 'y', 'z', 1, 2, 3]
Example: Concatenation More Lists
Similarly, the above example, will take place two times. First, it will append elements from list2 to list1 and then elements of list3 will be appended to list1.
list1 = ["x", "y" , "z"]
list2 = [1, 2, 3]
list3= ["bat", "cat", "rat"]
for x in list2:
list1.append(x)
for x in list3:
list1.append(x)
print(list1)
['a', 'b', 'c', 1, 2, 3, ‘bat’ , ‘cat’ , ‘rat’]
List Concatenation Using List Comprehension
List Concatenation can also be done using the list comprehension technique. In this case, a new list is created, this method is a one-liner alternative to the loop method.
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
# using list comprehension to concatenate
result = [y for x in [list1, list2] for y in x]
print ("Concatenated list using list comprehension: " + str(result))
Concatenated list using list comprehension: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
List Concatenation Using extend() Function
The extend()
is the function extended by lists in Python language and hence can be used to perform list concatenation operation. This function performs the in-place extension of the first list. It modifies the first list, unlike the + operator.
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
# using list.extend() to concat
list1.extend(list2)
print ("Concatenated list using list.extend() : " str(list1))
Concatenated list using list.extend() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
List Concatenation Using * Operator
This method is the new addition to list concatenation and uses the *
(asterisk) operator and works only in Python 3.6+ version. Any number of lists can be concatenated and returned in a new list by using this operator.
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
list3 = [2, 6, 8, 9, 0]
# using * operator to concat
result = [*list1, *list2, *list3]
print ("Concatenated list using * operator : " + str(result))
Concatenated list using * operator : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5, 2, 6, 8, 9, 0]
List Concatenation Using itertools.chain() function
If your lists are large and efficiency is a concern then itertools.chain()
function is useful. It returns the iterable after chaining its arguments in one and hence does not require to store the concatenated list if only its initial iteration is required. This is useful when the concatenated list has to be used just once.
import itertools
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
list3 = [7, 8, 9]
# using itertools.chain() to concat
result = list(itertools.chain(list1, list2, list3))
print ("Concatenated list using itertools.chain() : " + str(result))
Concatenated list using itertools.chain() : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5, 7, 8, 9]
Conclusion
In this article, we learned to concatenate two or multiple lists by using several built-in functions such as itertools.chain()
, extend()
, append()
, sum()
and operators like (+)
and (*)
. We used some custom codes as well. For example, we used list comprehensions to iterate the elements of the list and then add the elements to another list to perform concatenation.