# Creating a new tuple from an existing one

**URL:** <https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034>\
**Category:** General Usage\
**Tags:** tuple\
**Created:** [April 26, 2022, 7:18am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034 "2022-04-26T07:18:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [April 26, 2022, 7:18am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/1 "2022-04-26T07:18:06Z")

</div>

Is there a general way to create a new tuple from an existing one of any length where one the element is set to something else?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 26, 2022, 7:20am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/2 "2022-04-26T07:20:48Z")

</div>

One conceptually simple way (for **named tuples** ) is

```julia
julia> nt = (a=2, b=3, c=4)
(a = 2, b = 3, c = 4)

julia> nt2 = (nt..., b=10)
(a = 2, b = 10, c = 4)

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [April 26, 2022, 7:25am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/3 "2022-04-26T07:25:02Z")

</div>

There is `Base.setindex`:

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

julia> Base.setindex(tup, "abc", 2)
(1, "abc", 3)

```

Also, `Accessors.jl` is the package to do similar things, if more functionality is needed:

```julia
julia> using Accessors

julia> tup = (1, 2, 3)

julia> @set tup[2] = "abc"
(1, "abc", 3)

julia> @insert tup[2] = "abc"
(1, "abc", 2, 3)

```

---

<div class="post-metadata">

**Author:** ![johnbb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnbb/32/34233_2.png) [@johnbb](https://discourse.julialang.org/u/johnbb)\
**Post date:** [April 26, 2022, 7:26am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/4 "2022-04-26T07:26:37Z")

</div>

Great. Is there a similar way for unnamed tuples as well?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 26, 2022, 7:34am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/5 "2022-04-26T07:34:43Z")

</div>

Oh, sorry, I misread your question and thought you were interested in the named tuple case. For regular tuples I don’t know a nice oneliner from the top of my head.

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [April 26, 2022, 7:41am UTC](https://discourse.julialang.org/t/creating-a-new-tuple-from-an-existing-one/80034/6 "2022-04-26T07:41:17Z")

</div>

```julia
newtup(x,i,y) = (x[1:i-1]...,y,x[i+1:end]...)
newtup((1,2,3,4,5,6,7,8,9,10), 3, 11)

```

this seems to work
