Python Add Two Lists By Index Wise

How to add two lists in python, for example, index-wise summation of numbers? You can perform index-wise addition or summation of two lists by using list comprehension, map(), zip(), and manually adding by looping through lists.

Below are the methods to add two lists in Python:

  • Looping through elements and adding values index wise
  • Using list comprehension
  • Using map() & add function from the operator module
  • Using zip() & sum function with list comprehension

1. Quick Examples of Adding Two Lists

Following are quick examples of adding two lists with numbers index-wise.


# Quick examples of adding two lists

# Consider two lists
numbers1=[1,2,3,4,5,6]
numbers2=[2,4,6,8,5,3]

# Adding values from two lists
result=[]
for i in range(0, len(numbers1)):
    result.append(numbers1[i]+ numbers2[i])
print("Result: ",result)

# Using list comprehension
result = [numbers1[i] + numbers2[i] for i in range(len(numbers1))]
print(result)

# Using map()
from operator import add
result = list(map(add, numbers1, numbers2))
print(result)

# Using sum() & zip()
result = [sum(i) for i in zip(numbers1, numbers2)]
print(result)

2. Add Values from Two Lists in Python

To add two or multiple lists element-wise in Python you can easily do this by manually adding elements from each index by looping through the lists.

To do so, you need to use the range() to start the index from 0 until the end of the list. Also, make sure you have lists with the same length, having different lengths you will get errors.


# Consider two lists
numbers1=[1,2,3,4,5,6]
numbers2=[2,4,6,8,5,3]
print("numbers1: ",numbers1)
print("numbers2: ",numbers2)

# Adding values from two lists
result=[]
for i in range(0, len(numbers1)):
    result.append(numbers1[i]+ numbers2[i])
print("Result: ",result)

Yields below output.

python add lists

Here, first, we have initialized the result list and appended the elements after summation to this list. The len(numbers1) returns the length of the list.

3. Using List Comprehension to Add Two Lists

List comprehension is useful when working with large amounts of data. It is also helpful when we need to perform operations on multiple elements in a list like adding elements from two lists.


# Consider two lists
numbers1=[1,2,3,4,5,6]
numbers2=[2,4,6,8,5,3]
print("numbers1: ",numbers1)
print("numbers2: ",numbers2)

# Using list comprehension
result = [numbers1[i] + numbers2[i] for i in range(len(numbers1))]
print(result)

This example also yields the same output as above.

4. Using map()

The map() along with the add function from the operator can be used to add two lists in python. The map() is a built-in function that is used to apply a given function to each item of an iterable (e.g. list, tuple, etc.)


# Using map()
from operator import add
result = list(map(add, numbers1, numbers2))
print(result)

Here, map() returns the iterable of type map object and you need to convert it to a list by using list().

5. Using zip() + sum()

The zip() function takes in iterable as arguments and returns an iterator of tuples. This function pairs the elements together and returns them as tuples. And the sum() function adds the elements from the tuple.


# Using sum() & zip()
result = [sum(i) for i in zip(numbers1, numbers2)]
print(result)

Conclusion

In this Python article, you have learned how to add values from the two lists element-wise by looping through elements and adding values index-wise, using list comprehension, using map() & add function from the operator module, and finally using zip() & sum function with a list comprehension.

Related Articles

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Python Add Two Lists By Index Wise