← back

Vector Element-wise Sum

#121 · Linear Algebra · Easy

⊣ Solve on deep-ml.com

Problem

Given two vectors of equal length, compute their element-wise sum. Return a new list where each element is the sum of the corresponding elements from the two input vectors.

Solution

1
2
def vector_elementwise_sum(a: list[float], b: list[float]) -> list[float]:
    return [x + y for x, y in zip(a, b)]

Explanation

  1. Use zip to pair up corresponding elements from both vectors.
  2. Sum each pair using a list comprehension.
  3. Return the resulting list.

Complexity

  • Time: O(n) where n is the length of the vectors
  • Space: O(n) for the output list