Updated April 1, 2023
Introduction to Python Add List
In this article, the Python add list is defined as the list which can be added to another list to form a single list using a few functions that are provided by Python. Python add list can also be defined as the list containing the added value of elements of each given list. In general, python adds a list id the list having the concatenated list or the list with a summed value of the elements of each list given. This process of adding one list to another list there are different ways such as iterools.chain(), use of + and operator, * operator, extend(), append(), etc, and obtaining the add list having added value of elements using map(), zip(), sum(), etc.
In Python, we can add element values of one list to another list’s element value using different methods such as naïve method, map() function, etc.
Working on Python Add List
Working of adding items to list and adding list to another list with examples:
In this article, to add items to the list in Python we have various different methods such as append(), insert() and extend() functions. To add one list to another list in python there are different methods such as using the above same methods like extend(), using naïve, itertools.chain() methods, using + operator, and many more ways. In this article, we also see how to add the element value of one list to the element value of another list.
Let us in the below section about these methods for adding one list to another list with examples.
1. chain()
In Python, there is itertools module that is used for handling iterables like lists and chain() function is provided from this module to take input as any series of iterable and return only one single iterable. In general, itertools.chain() is used for concatenating the lists as it will take a series of lists as input and return one list as output. Let us demonstrate this in the below example.
Example
import itertools
print("Program to demonstrate concatenation of lists using itertools.chain()")
print("\n")
print("Two different lists for concatenation are as follows")
l1 = ['e', 'd', 'u', 'c', 'b', 'a']
print(l1)
print("\n")
l2 = [9, 8, 7, 6, 5]
print(l2)
print("\n")
lst_out = list(itertools.chain(l1, l2))
print("The concatenated list is as follows:")
print(str(lst_out))
Output:
In the above program, we can see using itertools.chain() function we can concatenate two lists which means we can add one list to another list.
2. Using + and * operator
In Python, we can use both + and * operator for adding one list to another list. Using + operator is a very common method used for concatenation of two lists. Using * operator is a newly added method for concatenation of lists and therefore this method is applicable for the python 3.6 and above version only. Let us demonstrate these in the below example.
Example
import itertools
print("Program to demonstrate concatenation of lists using itertools.chain()")
print("\n")
print("Two different lists for concatenation are as follows")
l1 = [1, 2, 3, 4]
print(l1)
print("\n")
l2 = [9, 8, 7, 6, 5]
print(l2)
print("\n")
lst_out1 = l1 + l2
print("The concatenated list using + operator is as follows:")
print(str(lst_out1))
'''
lst_out2= [*l1, * l2]
print("The concatenated list using * operator is as follows:")
print(str(lst_out2))
'''
Output:
In the above program, we can see that in the output screenshot we have obtained output for only + operator only as *operator works only in python version 3.6 and above but the code is provided in the program as to how to use * operator for adding two lists.
In Python, there are different ways for adding one list to another list or concatenation of lists using extend(), append(), etc. Now we will see how to add element values of one list to another element value list in Python. Let us see various methods to do this in Python in the below section.
3. Using map()
In Python, map() operation can be used for adding element values of given lists but the only map() cannot be used we must use add() function along with a map() then we can add the element values of one list to another list. Let us demonstrate it below
Example
from operator import add
print("Program to demonstrate addition of element value of one list to another list using map()")
print("\n")
print("Two different lists for concatenation are as follows")
l1 = [7, 2, 5, 3]
print(l1)
print("\n")
l2 = [9, 8, 7, 6]
print(l2)
print("\n")
lst_out = list(map(add, l1, l2))
print("The addition of element values of list is as follows:")
print(str(lst_out))
Output:
In the above program, we can see the addition of the two list element values. And the output can be seen in the above screenshot.
4. Using zip() and sum()
In Python, similar to the above functions there are other functions that can be used for adding element value of lists using zip() and sum() functions. To add element value of lists first we have to sum the values using sum() function and then zip all the result and display using zip() function. Let us demonstrate it below:
Example
from operator import add
print("Program to demonstrate addition of element value of one list to another list using sum() and zip()")
print("\n")
print("Two different lists for addition are as follows")
l1 = [7, 2, 5, 3]
print(l1)
print("\n")
l2 = [9, 8, 7, 6]
print(l2)
print("\n")
lst_out = [sum(i) for i in zip(l1, l2)]
print("The addition of element values of list is as follows:")
print(str(lst_out))
Output:
In the above program, we can see we are using the sum() function which we will iterate through each element of the list by traversing using the index of the list that is given to each element of the list and then using the zip() function we are zipping the result of the two lists together as shown in the above screenshot.
In Python, there are other different methods for adding the element values of one list to another list such as using shorthand technique which is quicker such as list comprehensions, naïve method which uses append(), and + operator.
Conclusion
In this article, we conclude that in python list is one iterable which is a very important data structure. Therefore in this article, we have seen various methods for adding one list to another list or concatenation of two lists using itertools module with chain() function and using + and * operator. In this article, we also saw how to add values of the items in one list to the values of items in another list using methods like map(), add(), sum(), and zip() function in Python.
Recommended Articles
This is a guide to Python Add List. Here we discuss the introduction and working of python add list along with different examples and its code implementation. You may also have a look at the following articles to learn more –