# Insert Array into another

**URL:** https://discourse.julialang.org/t/insert-array-into-another/65836
**Category:** New to Julia
**Tags:** question, arrays
**Created:** [August 4, 2021, 7:34pm UTC](https://discourse.julialang.org/t/insert-array-into-another/65836 "2021-08-04T19:34:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![victormororo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victormororo/32/27847_2.png) [@victormororo](https://discourse.julialang.org/u/victormororo)
#### Post date: [August 4, 2021, 7:34pm UTC](https://discourse.julialang.org/t/insert-array-into-another/65836/1 "2021-08-04T19:34:18Z")

</div>

is there already something like this?

```julia
function insert!(a::Array{T,1}, i::Integer, item::Array{T,1}) 
    _growat!(a, i, length(item))
    bd = i + length(item) - 1
    @inbounds for _i in i:bd a[_i] = item[_i-i+1] end
    return a
end

```

[source to compare](https://github.com/JuliaLang/julia/blob/master/base/array.jl)

---

<div class="post-metadata">

### Author: ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)
#### Post date: [August 4, 2021, 7:43pm UTC](https://discourse.julialang.org/t/insert-array-into-another/65836/2 "2021-08-04T19:43:11Z")

</div>

Maybe [`splice!`](https://docs.julialang.org/en/v1/base/collections/#Base.splice!) ?

> ```julia
> splice!(a::Vector, index::Integer, [replacement]) -> item
> 
> ```
> 
> …  
> To insert `replacement` before an index `n` without removing any items, use `splice!(collection, n:n-1, replacement)`.

---

<div class="post-metadata">

### Author: ![victormororo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/victormororo/32/27847_2.png) [@victormororo](https://discourse.julialang.org/u/victormororo)
#### Post date: [August 4, 2021, 8:04pm UTC](https://discourse.julialang.org/t/insert-array-into-another/65836/3 "2021-08-04T20:04:44Z")

</div>

oh thank you!  
i had not seen this one hehe, started with Julia yesterday

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 23, 2021, 11:30am UTC](https://discourse.julialang.org/t/insert-array-into-another/65836/4 "2021-10-23T11:30:30Z")

</div>

@oxinabox, sorry but do not fully understand how to properly use `splice!()` to insert one array into another.

It doesn’t seem right to have to loop and adjust the indices after each insertion. A simple (perhaps too simple) example of inserting `Y` into `X`:

```julia
X = [11, 13, 15, 17]
Y = [12, 14, 16]
ix = [2, 3, 4]

k = 0
for (i,y) in zip(ix, Y)
    splice!(X, i+k:i+k-1, y)
    k += 1
end

julia> X
7-element Vector{Int64}:
 11
 12
 13
 14
 15
 16
 17

```
