# Sum of two vectors of different lengths

**URL:** https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751
**Category:** New to Julia
**Tags:** for-loop
**Created:** [September 4, 2022, 11:47am UTC](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751 "2022-09-04T11:47:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![moataz-sabry](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moataz-sabry/32/36856_2.png) [@moataz-sabry](https://discourse.julialang.org/u/moataz-sabry)
#### Post date: [September 4, 2022, 11:47am UTC](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751/1 "2022-09-04T11:47:49Z")

</div>

I’m looking for a short for loop to sum two vectors of different lengths as in the following MWE. In general `y` could be longer, shorter than `x` or equal to it in length.

```julia
julia> x = [1, 2, 3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> y = [5, 6]
2-element Vector{Int64}:
 5
 6

julia> sum(i + j for (i, j) in (x, y))
14

```

it stops after considering the two elements of `y` and returns `14` as result. is there a way to make the loop continue after finishing `y`, so that the result would be `21`?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 4, 2022, 11:50am UTC](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751/2 "2022-09-04T11:50:34Z")

</div>

```julia
julia> sum(x) + sum(y)
21

```

---

<div class="post-metadata">

### Author: ![owiecc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/owiecc/32/8894_2.png) [@owiecc](https://discourse.julialang.org/u/owiecc)
#### Post date: [September 4, 2022, 11:50am UTC](https://discourse.julialang.org/t/sum-of-two-vectors-of-different-lengths/86751/3 "2022-09-04T11:50:39Z")

</div>

`sum(x) + sum(y)`
