element-wise addition
python lists
list operations
programming
python tutorials

Element-wise addition of 2 lists?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Element-wise addition means adding the first item of one list to the first item of another, the second to the second, and so on. In Python, the cleanest solution is usually zip(), but the right approach depends on whether the lists must be equal in length, whether you want padding, and whether you are working with numeric arrays.

The Basic Python Solution

For ordinary Python lists of equal length, use a list comprehension with zip():

python
1list_a = [2, 4, 6]
2list_b = [1, 3, 5]
3
4result = [a + b for a, b in zip(list_a, list_b)]
5print(result)

Output:

python
[3, 7, 11]

This is the idiomatic answer because it is short, readable, and works for any types that support +.

Why list_a + list_b Is Wrong

A common beginner mistake is assuming list addition means numeric addition:

python
print([1, 2] + [3, 4])

Output:

python
[1, 2, 3, 4]

For Python lists, + means concatenation, not component-wise math. If you need pairwise sums, you must iterate over both lists together.

Handle Unequal Lengths Carefully

zip() stops at the shorter input. That behavior is sometimes helpful, but sometimes it hides bugs.

python
1list_a = [10, 20, 30]
2list_b = [1, 2]
3
4result = [a + b for a, b in zip(list_a, list_b)]
5print(result)

Output:

python
[11, 22]

The last item from list_a is ignored. If you require equal lengths, validate first:

python
1def add_lists(a, b):
2    if len(a) != len(b):
3        raise ValueError("Both lists must have the same length")
4    return [x + y for x, y in zip(a, b)]

That makes the contract explicit.

Padding Missing Values

If you want to keep the longer list and treat missing entries as zero, use itertools.zip_longest:

python
1from itertools import zip_longest
2
3list_a = [10, 20, 30]
4list_b = [1, 2]
5
6result = [a + b for a, b in zip_longest(list_a, list_b, fillvalue=0)]
7print(result)

Output:

python
[11, 22, 30]

This is useful when one data source may be shorter but the missing values should behave like defaults.

If You Need Speed, Use NumPy

For heavy numeric work, plain lists are not ideal. NumPy performs element-wise operations natively and much faster on large arrays.

python
1import numpy as np
2
3a = np.array([2, 4, 6])
4b = np.array([1, 3, 5])
5
6result = a + b
7print(result)

Output:

python
[ 3  7 11]

NumPy also supports broadcasting, which plain Python lists do not:

python
print(np.array([1, 2, 3]) + 10)

That makes it the right tool for scientific and data-processing workloads.

Other Useful Variants

If you want a lazy iterator rather than a full list, combine map() and zip():

python
result = map(sum, zip([1, 2, 3], [4, 5, 6]))
print(list(result))

This is slightly more functional in style, though many Python developers find the list comprehension clearer.

You can also sum more than two lists:

python
result = [x + y + z for x, y, z in zip([1, 2], [3, 4], [5, 6])]
print(result)

The same pattern scales well as long as all lists are aligned by position.

Common Pitfalls

  • Using list_a + list_b and expecting numeric addition. That concatenates the lists.
  • Forgetting that zip() stops at the shortest input.
  • Ignoring length mismatches when equal-sized inputs are required by the problem.
  • Using plain Python lists for large numerical workloads where NumPy would be faster and clearer.
  • Assuming this technique only works for integers. It works for any values whose types support +.

Summary

  • Use [a + b for a, b in zip(list_a, list_b)] for ordinary element-wise addition in Python.
  • '+ on lists concatenates; it does not add item by item.'
  • Validate lengths if truncation would be a bug.
  • Use zip_longest() when missing values should be padded.
  • Use NumPy for large-scale numeric data or broadcasting behavior.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.