How to add 2 lists in Python?
In Python, a list is defined as a data structure that contains a sequence of elements.
It can contain any kind of data type inside it but in order to concatenate two lists, the lists must be of the similar data types. A user can have many lists in a code, which sometimes we might need to merge, join, or concatenate. Merging a list is very useful in many ways, and using this elaboration, we shall see the various ways by which we can concatenate two lists in Python.
In this article, we will learn how to add two lists in Python. Adding or Concatenating 2 lists in Python can be done in many ways such as:
- Concatenate two lists in Python through append()
- Concatenate two lists in Python using the + operator
- Concatenate two lists in Python using the List comprehension
- Using extend() method
- Using * operator
- Using itertools.chain()
1. Using append() function
Using this way, we first traverse the second list and then append all the elements in the first list one by one, so that the first list has all the elements added into it.
Example:
# concatenation using append()
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
fori in list2 :
list1.append(i);
# Printing new list
print ("Concatenated list is : "+ str(list1))
Output:
Concatenated list is : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
2. Using + operator
The most convenient method to perform a list concatenation is the use of “+” operator. It can easily add the whole list behind the other list in one go and hence perform the concatenation.
Example:
# list addition using + operator
# Initialize lists
list3 = [1, 4, 5, 6, 5]
list4 = [3, 5, 7, 2, 5]
list3 = list3 + list4
# Printing concatenated list
print ("New list is : "+ str(list3))
Output:
New list is : [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
3. By list Comprehension
List comprehension also can complete the task of list concatenation. In this case, a new list is created.
Example:
# adding list using list comprehension
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
new_list = [y for x in [list1, list2] for y in x]
# Printing new list
print ("new list using list comprehension is: "+ str(new_list))
Output:
new list using list comprehension is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
4. Using extend()
The extend() is the function used in lists by Python therefore can be used to perform this action. This function extends the first list by adding the second list to it.
Example:
# concatenation using extend()
# Initialize lists
list3 = [1, 4, 5, 6, 5]
list4 = [3, 5, 7, 2, 5]
list3.extend(list4)
# Printing new list
print ("New list using list.extend() is: "+ str(test_list3))
Output:
New list using list.extend() is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
5. Using * operator
The * operator is the new addition method for list concatenation and works only for Python 3.6+. This operator can concatenate any number of lists by returning a new list.
Example:
# adding lists using * operator
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
new_list = [*list1, *list2]
# Printing new list
print ("new list using * operator is: "+ str(new_list))
Output:
new list using * operator is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]
6. Using itertools.chain()
The itertools.chain() 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 concatenated list has to be used just once.
Example:
# adding lists using itertools.chain()
importitertools
# Initialize lists
list1 = [1, 4, 5, 6, 5]
list2 = [3, 5, 7, 2, 5]
# using itertools.chain() to concat
new_list = list(itertools.chain(list1, list2))
# Printing concatenated list
print ("new list using itertools.chain() : "+ str(new_list))
Output:
new list using itertools.chain() is: [1, 4, 5, 6, 5, 3, 5, 7, 2, 5]