# How to sum array of tuples

**URL:** <https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378>\
**Category:** General Usage\
**Tags:** array, tuple\
**Created:** [October 31, 2020, 6:03pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378 "2020-10-31T18:03:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [October 31, 2020, 6:03pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/1 "2020-10-31T18:03:09Z")

</div>

I have an array of tuples of weight and bias gradients for my neural network (so, an array of matrix-vector tuples). How can I sum over them, ideally without an explicit loop?

As a small example, let

```julia
arr = [(rand(2,2), rand(2)), (rand(2,2), rand(2))]

```

I tried using `sum(arr)`, but that didn’t work since addition is not defined for tuples. I tried broadcasting with `sum.(arr)`, but that gave a dimension mismatch error.

Currently, I’m using `sum(first.(arr)), sum(last.(arr))`, but this seems inefficient and isn’t extensible to larger tuples. I don’t have Julia 1.6 installed, but based on the NEWS.md it seems like I might be able to use something like `reduce(.+, arr)`. Is there an better solution that works for v1.5?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 31, 2020, 6:16pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/2 "2020-10-31T18:16:20Z")

</div>

> [@derikk](#):
>
> I don’t have Julia 1.6 installed, but based on the NEWS.md it seems like I might be able to use something like `reduce(.+, arr)` . Is there an better solution that works for v1.5?

```julia
reduce((x, y) -> x .+ y, arr)

```

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [October 31, 2020, 6:33pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/3 "2020-10-31T18:33:58Z")

</div>

Does this work for you? (untested)

```julia
Iterators.zip(arr...) .|> sum

```

---

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [October 31, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/4 "2020-10-31T18:39:14Z")

</div>

> [@pdeffebach](#):
>
> `reduce((x, y) -> x .+ y, arr)`

Thanks, this seems to do the job quite nicely! Am I correct that the lambda can just be replaced with a `.+` in version 1.6?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [October 31, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/5 "2020-10-31T18:39:29Z")

</div>

I believe so, yes.

---

<div class="post-metadata">

**Author:** ![derikk](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@derikk](https://discourse.julialang.org/u/derikk)\
**Post date:** [October 31, 2020, 6:39pm UTC](https://discourse.julialang.org/t/how-to-sum-array-of-tuples/49378/6 "2020-10-31T18:39:52Z")

</div>

This works, but I decided not to use it because it is less memory-efficient.
