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.
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():
Output:
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:
Output:
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.
Output:
The last item from list_a is ignored. If you require equal lengths, validate first:
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:
Output:
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.
Output:
NumPy also supports broadcasting, which plain Python lists do not:
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():
This is slightly more functional in style, though many Python developers find the list comprehension clearer.
You can also sum more than two lists:
The same pattern scales well as long as all lists are aligned by position.
Common Pitfalls
- Using
list_a + list_band 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
- elif in list comprehension conditionals
- Eliminating cyclic flows from a graph
- Empty set literal?
- Emulating Amazon SQS during development
- Else clause on Python while statement
- Emacs bulk indent for Python
- Ensuring a partially connected digraph is strongly connected
- Enumerating all paths in a directed acyclic graph

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 courseTrack 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.