# Is there a way to unpack like a,\*rest=\[1,2,3,4\] in python?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572>\
**Category:** General Usage\
**Created:** [March 7, 2019, 1:51pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572 "2019-03-07T13:51:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gu\_Junhua](https://avatars.discourse-cdn.com/v4/letter/g/48db29/32.png) [@Gu\_Junhua](https://discourse.julialang.org/u/Gu_Junhua)\
**Post date:** [March 7, 2019, 1:51pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572/1 "2019-03-07T13:51:07Z")

</div>

Hi, I’m trying to migrate a python program to Julia, I find array unpacking very convenient. I wonder is there something similar in Julia?

In python we can write

```julia
a=[1,2,3,4,5]
b,*p=a

```

then `b=1` and `p=[2,3,4,5]`

and similarly

```julia
a=[1,2,3,4,5,6,7]
*p, b=a

```

then `p=[1,2,3,4,5,6]` and `b=7`

Is there similar thing in Julia?  
Thanks

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 7, 2019, 1:52pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572/2 "2019-03-07T13:52:23Z")

</div>

There is not: [https://github.com/JuliaLang/julia/issues/2626](https://github.com/JuliaLang/julia/issues/2626).

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [March 7, 2019, 8:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572/3 "2019-03-07T20:05:17Z")

</div>

It’s not the prettiest syntax, but you can do

```julia
julia> import Pkg; Pkg.add("Match");

julia> using Match

julia> a = [1,2,3,4,5]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> @match(a, [b, p..., y])
(1, [2, 3, 4], 5)

julia> b
1

julia> p
3-element view(::Array{Int64,1}, 2:4) with eltype Int64:
 2
 3
 4

julia> y
5

```

It also should be possible to create a macro that would handle the syntax proposed in [https://github.com/JuliaLang/julia/issues/2626](https://github.com/JuliaLang/julia/issues/2626).

Cheers,  
Kevin

---

<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:** [March 7, 2019, 8:52pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572/4 "2019-03-07T20:52:42Z")

</div>

Of course, you can do

```julia
b = a[1]
p = a[2:end]

```

and so on, so there is a _way_ to unpack this, but no special syntax (without using macros).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 8, 2019, 11:50am UTC](https://discourse.julialang.org/t/is-there-a-way-to-unpack-like-a-rest-1-2-3-4-in-python/21572/5 "2019-03-08T11:50:35Z")

</div>

You don’t need macros or special syntax for this, just a trivial mini-function.

```julia
splitfirst(x) = x[1], x[2:end]
a = 1:5
b, p = splitfirst(a)

```
