Addition of two list in python

This tutorial covers the following topic – Python Add Two list Elements. It describes four unique ways to add the list items in Python. For example – using a for loop to iterate the lists, add corresponding elements, and store their sum at the same index in a new list. Some of the other methods you can use are using map[] and zip[] methods.

All of these procedures use built-in functions in Python. However, while using the map[], you’ll require the add[] method, and zip[] will need to be used with the sum[] function. Both these routines are defined in the operator module, so you would have to import it in your program. After finishing up this post, you can assess which of these ways is more suitable for your scenario.

By the way, it will be useful if you have some elementary knowledge about the Python list. If not, please do go through the linked tutorial.

Python Add Two List  Elements – 4 Unique Ways

For loop to add elements of two lists

It is the simplest approach in Python to add two list elements. In this method, a for loop is used to iterate the smaller of the two lists. In every iteration, add the corresponding values at the running index from the two lists, and insert the sum in a new list.

# Python add two list elements using for loop # Setting up lists in_list1 = [11, 21, 34, 12, 31, 26] in_list2 = [23, 25, 54, 24, 20] # Display input lists print ["\nTest Input: **********\n Input List [1] : " + str[in_list1]] print [" Input List [2] : " + str[in_list2]] # Using for loop # Add corresponding elements of two lists final_list = [] # Choose the smaller list to iterate list_to_iterate = len[in_list1] < len[in_list2] and in_list1 or in_list2 for i in range[0, len[list_to_iterate]]: final_list.append[in_list1[i] + in_list2[i]] # printing resultant list print ["\nTest Result: **********\n Smaller list is : " + str[list_to_iterate]] print [" Resultant list is : " + str[final_list]]

The above program will yield the following output:

List comprehension to add elements of two lists

The list comprehension is a unique shorthand technique in Python to create lists at runtime. It lets the programmer perform any operation on the input list elements.

This method is also a bit faster for manipulating the list data structure. Check out the below sample Python program.

# Python add two list elements using list comprehension # Setting up lists in_list1 = [11, 21, 34, 12, 31] in_list2 = [23, 25, 54, 24, 20, 27] # Display input lists print ["\nTest Input: **********\n Input List [1] : " + str[in_list1]] print [" Input List [2] : " + str[in_list2]] # Using list comprehension # Add corresponding elements of two lists final_list = [] # Choose the smaller list to iterate list_to_iterate = len[in_list1] < len[in_list2] and in_list1 or in_list2 final_list = [in_list1[i] + in_list2[i] for i in range[len[list_to_iterate]]] # printing resultant list print ["\nTest Result: **********\n Smaller list is : " + str[list_to_iterate]] print [" Resultant list is : " + str[final_list]]

This program will produce the following result:

map[] and add[] functions to add given lists

map[] is one of the higher-order functions in Python. It takes another method as its first argument, along with the two lists.

Both the input lists are passed to the input function [add[] in our case] as parameters. This function adds up the elements of two lists and returns an iterable as output.

We convert the iterable into a list using the list constructor method. Check out the complete example below:

# Python add two list elements using map[] and add[] from operator import add # Setting up lists in_list1 = [11, 21, 34, 12, 31] in_list2 = [23, 25, 54, 24, 20, 27] # Display input lists print ["\nTest Input: **********\n Input List [1] : " + str[in_list1]] print [" Input List [2] : " + str[in_list2]] # Using map[] and add[] # Add corresponding elements of two lists final_list = list[map[add, in_list1, in_list2]] # printing resultant list print ["\nTest Result: **********\n Resultant list is : " + str[final_list]]

This example will give the following outcome:

zip[] and sum[] functions to add given lists

The sum[] function adds list elements one by one using the index. And the zip[] function groups the two list items together.

This approach is also an elegant way to achieve – Python Add two list elements. Check out the full example with source code below:

# Python add two list elements using zip[] and sum[] # Setting up lists in_list1 = [11, 21, 34, 12, 31, 77] in_list2 = [23, 25, 54, 24, 20] # Display input lists print ["\nTest Input: **********\n Input List [1] : " + str[in_list1]] print [" Input List [2] : " + str[in_list2]] # Using zip[] and sum[] # Add corresponding elements of two lists final_list = [sum[i] for i in zip[in_list1, in_list2]] # printing resultant list print ["\nTest Result: **********\n Resultant list is : " + str[final_list]]

The above code will provide the following output:

You’ve seen various methods here for Python Add two list elements. Now, you can choose any of them based on your business case. By the way, to learn Python from scratch to depth, do read our step by step Python tutorial.

PythonServer Side ProgrammingProgramming

Lists can be added in python resulting in creation of a new list containing elements from both the lists. There are various approaches to add two lists and they are described below. But in all these cases the lists must be of same length.

Using Append[]

Using append[] we can add the elements of one list to another.

Example

 Live Demo

List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] # printing original lists print ["list1 : " + str[List1]] print ["list2 : " + str[List2]] newList = [] for n in range[0, len[List1]]:    newList.append[List1[n] + List2[n]] print[newList]

Running the above code gives us the following result:

list1 : [7, 5.7, 21, 18, 2.6666666666666665] list2 : [9, 15, 6.2, 0.3333333333333333, 11] [16, 20.7, 27.2, 18.333333333333332, 13.666666666666666]

Using Map[] and Add[]

We can sue the map[] along with the add[] to add the elements of the list. The map function uses the first parameter which the add function and adds the elements of two lists which are at the same index.

Example

 Live Demo

from operator import add #Adding two elements in the list. List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] # printing original lists print ["list1 : " + str[List1]] print ["list2 : " + str[List2]] NewList = list[map[add,List1,List2]] print[NewList]

Running the above code gives us the following result:

list1 : [7, 5.7, 21, 18, 2.6666666666666665] list2 : [9, 15, 6.2, 0.3333333333333333, 11] [16, 20.7, 27.2, 18.333333333333332, 13.666666666666666]

Using ZIp[] and Sum[]

In a similar approach as above we can take the zip[] and the sum[] using a for loop. Through the for loop we bind two elements of a list at the same index and then apply the sum[] to each of them.

Example

 Live Demo

#Adding two elements in the list. List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] result = [sum[n] for n in zip[List1, List2]] print[result]

Running the above code gives us the following result:

[16, 20.7, 27.2, 18.333333333333332, 13.666666666666666]

Published on 30-Dec-2019 10:14:57

Video liên quan

Chủ Đề