# Summing an element of tuple

**URL:** <https://discourse.julialang.org/t/summing-an-element-of-tuple/84020>\
**Category:** Performance\
**Tags:** question, tuple\
**Created:** [July 10, 2022, 3:19pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020 "2022-07-10T15:19:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salvatore\_Candita](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salvatore_candita/32/20477_2.png) [@Salvatore\_Candita](https://discourse.julialang.org/u/Salvatore_Candita)\
**Post date:** [July 10, 2022, 3:19pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/1 "2022-07-10T15:19:10Z")

</div>

Hi everyone,  
I’m struggling with the following operation: I have an array of Tuple{Int,String) in which I have to increase by 1 the first element of the tuple but it seems doesn’t work in a while cycle; here the solution I implemented

> s=Tuple{Float64,String}  
> for i in 1:n  
> push!(s, (0.0,“null”) )  
> end  
> for i in 1:n  
> s[i][1]+=1  
> end

it gives me error on the setindex, how I’m supposed to do this?Thanks everyone

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 10, 2022, 3:49pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/2 "2022-07-10T15:49:35Z")

</div>

Tuple is immutable, you can’t change value in it, also please post you code in code block

````julia
```
your code
```

````

---

<div class="post-metadata">

**Author:** ![Salvatore\_Candita](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salvatore_candita/32/20477_2.png) [@Salvatore\_Candita](https://discourse.julialang.org/u/Salvatore_Candita)\
**Post date:** [July 10, 2022, 3:55pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/3 "2022-07-10T15:55:21Z")

</div>

Oh yes, what a stupid error, I suppose have to use another structure 🙂  
Thank you so much and sorry for the bad wrong formatting of the code

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [July 10, 2022, 5:07pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/4 "2022-07-10T17:07:42Z")

</div>

I would suggest a 1D array, also known as Vector.

I am however struggling to see the use-case. Perhaps there is a better approach to what you are tying to do? Perhaps you would find the `enumerate` function or a `1:n` range useful.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [July 11, 2022, 12:05am UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/5 "2022-07-11T00:05:01Z")

</div>

Since a Tuple is an immutable container, you can’t change it’s contents once it’s created. You can get around this by creating a new Tuple with the modified contents.

```julia
s = Tuple{Float64,String}[]
for i = 1:n
    push!(s, (0.0, "null"))
end

for i = 1:n
    s[i] = (s[i][1]+1,s[i][2])
end

```

In rare cases when you have to change the contents after the tuple was created, you can still change the underlying contents of mutable containers inside tuples. But in this case, maybe choosing an Array from the beginning could be better.

```julia
s = Tuple{Vector{Float64},String}[]
for i = 1:n
    push!(s, ([0.0],"null"))
end

for i = 1:n
    s[i][1] .+= 1
end

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 11, 2022, 2:47am UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/6 "2022-07-11T02:47:35Z")

</div>

BTW, you can simplify this kind of tuple replacement with `Base.setindex`:

```julia
julia> x = (1, 2, 3)
(1, 2, 3)

julia> y = Base.setindex(x, 5, 2) # Create a new tuple like `x` but with its second entry set to 5
(1, 5, 3)

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [July 11, 2022, 10:37am UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/7 "2022-07-11T10:37:48Z")

</div>

It’s great to know about such a function; I had no idea it existed because it’s not exported. I usually see it when extending functionality of structs like `import Base.setindex; setindex(..) = ..`.

I think it is a bit confusing, though, because the order of the arguments is not what I would expect. If I’m reading it, I’d compare with the equivalent `x[2] = 5`, so I’m expecting `setindex(x, 2, 5)`. This is similar to, for example, `copyto!(A, Ainds, B, Binds)` which means `A[Ainds] = B[Binds]`.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [July 11, 2022, 12:08pm UTC](https://discourse.julialang.org/t/summing-an-element-of-tuple/84020/8 "2022-07-11T12:08:42Z")

</div>

Arguments to `setindex` are just like `getindex`, `setindex!`, `view`. The index always comes last, because for multi-dimensional objects there will be more than one of them.
