# "unzip" a vector of Pairs

**URL:** https://discourse.julialang.org/t/unzip-a-vector-of-pairs/1346
**Category:** General Usage
**Tags:** question
**Created:** [January 7, 2017, 3:55pm UTC](https://discourse.julialang.org/t/unzip-a-vector-of-pairs/1346 "2017-01-07T15:55:03Z")
**Posts on this page:** 3
**Page:** 1

<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: [January 7, 2017, 3:55pm UTC](https://discourse.julialang.org/t/unzip-a-vector-of-pairs/1346/1 "2017-01-07T15:55:03Z")

</div>

Given a `Vector` of `Pair`s, what’s an idiomatic way of getting a vector of first and second elements in each `Pair`? MWE:

```julia
x = [Pair(x,2*x) for x in 1:5]

```

then

```julia
julia> collect.(collect(zip(((a,b) for (a,b) in x)...))) # works, but convoluted
2-element Array{Array{Int64,1},1}:
 [1,2,3,4,5] 
 [2,4,6,8,10]

```

---

<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: [January 7, 2017, 4:19pm UTC](https://discourse.julialang.org/t/unzip-a-vector-of-pairs/1346/3 "2017-01-07T16:19:04Z")

</div>

> [@Tamas\_Papp](#):
>
> Given a Vector of Pairs, what’s an idiomatic way of getting a vector of first and second elements in each Pair?

`first.(x)` and `last.(x)` works pretty well and is terse.

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [January 7, 2017, 4:20pm UTC](https://discourse.julialang.org/t/unzip-a-vector-of-pairs/1346/4 "2017-01-07T16:20:38Z")

</div>

One possibility is

```julia
julia> [first(p) for p in x]
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

julia> [last(p) for p in x]
5-element Array{Int64,1}:
  2
  4
  6
  8
 10

```

EDIT:  
Which can be written as `map(first, x)` and `map(last, x)`

I do agree that it is counterintuitive / unfortunate that two loops through are required.
