# List of pairs

**URL:** <https://discourse.julialang.org/t/list-of-pairs/75564>\
**Category:** General Usage\
**Tags:** tuple, arrays\
**Created:** [February 1, 2022, 9:26am UTC](https://discourse.julialang.org/t/list-of-pairs/75564 "2022-02-01T09:26:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [February 1, 2022, 9:26am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/1 "2022-02-01T09:26:22Z")

</div>

Hi  
How I can create a list of pairs, something like  
A[1]=[(1,2),(1,5),(2,4)]  
A[2]=[(3,4),(2,5),(3,6)]  
and append new pairs to it and access the element of it ??

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2022, 9:47am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/2 "2022-02-01T09:47:28Z")

</div>

Could you clarify your question? First of all, you aren’t using any `Pair`s in your question:

```julia
julia> typeof((1, 2))
Tuple{Int64, Int64}

julia> typeof(1 => 2)
Pair{Int64, Int64}

```

`(1, 2)` is a `Tuple`, while `Pair`s are created with the `=>` infix operator.

`append!` works as expected for vectors of pairs:

```julia
julia> x = [1 => 2, 1 => 5]
2-element Vector{Pair{Int64, Int64}}:
 1 => 2
 1 => 5

julia> append!(x, [3 => 4, 2 =>5])
4-element Vector{Pair{Int64, Int64}}:
 1 => 2
 1 => 5
 3 => 4
 2 => 5

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 1, 2022, 9:55am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/3 "2022-02-01T09:55:31Z")

</div>

Maybe @ahmad88 was using “pair” in the general/mathematical sense of a two-element tuple. Using `append!` as suggested by @nilshg also works in that case:

```julia
julia> A = [(1,2), (1,5), (2,4)]
3-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (1, 5)
 (2, 4)

julia> append!(A, [(3,4), (2,5), (3,6)])
6-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (1, 5)
 (2, 4)
 (3, 4)
 (2, 5)
 (3, 6)

```

To append a single element (a single tuple) use `push!`:

```julia
push!(A, (4,7))

```

You can index the vector with `A[i]` to get the i-th element:

```julia
julia> A[3]
(2, 4)

```

---

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [February 1, 2022, 9:55am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/4 "2022-02-01T09:55:50Z")

</div>

I am so sorry, my question was about Tuple , how I can create list of tuples and append to it and read from it

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2022, 10:10am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/5 "2022-02-01T10:10:14Z")

</div>

Okay, thanks for clarifying. @sijo has shown you how my example works just as well with tuples, although your original question actually seemed to be based on a _vector of a vector of tuples_, which is different from a _vector of tuples_:

```julia
julia> A = [[(1, 2), (1, 5), (2, 4)]] # note the double brackets here
1-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(1, 2), (1, 5), (2, 4)]

julia> push!(A, [(3, 4), (2, 5), (3, 6)]) # we are now pushing a vector of tuples, not the tuples themselves
2-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(1, 2), (1, 5), (2, 4)]
 [(3, 4), (2, 5), (3, 6)]

julia> A[2] # each element in A is now a vector of tuples
3-element Vector{Tuple{Int64, Int64}}:
 (3, 4)
 (2, 5)
 (3, 6)

```

---

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [February 1, 2022, 10:14am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/6 "2022-02-01T10:14:13Z")

</div>

Thank you so much, but how I can create a list with index and be empty at the beginning  
A[1]=  
then  
append!(A[1], [(3,4), (2,5), (3,6)])  
or  
push!(A[1], (4,7))  
is it possible like this ??

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2022, 10:21am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/7 "2022-02-01T10:21:59Z")

</div>

Both of these just work?

```julia
julia> append!(A[1], [(3, 4), (2, 5), (3, 6)])
6-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (1, 5)
 (2, 4)
 (3, 4)
 (2, 5)
 (3, 6)

julia> push!(A[1], (4, 7))
7-element Vector{Tuple{Int64, Int64}}:
 (1, 2)
 (1, 5)
 (2, 4)
 (3, 4)
 (2, 5)
 (3, 6)
 (4, 7)

```

If you want to initialise an empty vector of Tuples, just write that:

```julia
julia> x = Tuple{Int64, Int64}[]
Tuple{Int64, Int64}[]

julia> push!(x, (2, 3))
1-element Vector{Tuple{Int64, Int64}}:
 (2, 3)

```

---

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [February 1, 2022, 10:37am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/8 "2022-02-01T10:37:10Z")

</div>

I am sorry I think my question is not clear enough, what if I want to create 10 different lists with name A but with different indexes like

```julia
for i in range 1:10
    x[i] = Tuple{Int64, Int64}[]
end

```

then start appending and pushing to these lists based on the index  
`push!(x[5], (2, 3))`  
so (2,3) will be the first pair in the 5th list

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2022, 10:43am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/9 "2022-02-01T10:43:27Z")

</div>

But this is exactly what I showed above, isn’t it? You can simplify your loop with a comprehension:

```julia
julia> A = [Tuple{Int64, Int64}[] for _ ∈ 1:10]
10-element Vector{Vector{Tuple{Int64, Int64}}}:
 []
 []
 []
 []
 []
 []
 []
 []
 []
 []

julia> push!(A[5], (2, 3))
1-element Vector{Tuple{Int64, Int64}}:
 (2, 3)

julia> A
10-element Vector{Vector{Tuple{Int64, Int64}}}:
 []
 []
 []
 []
 [(2, 3)]
 []
 []
 []
 []
 []

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 1, 2022, 10:55am UTC](https://discourse.julialang.org/t/list-of-pairs/75564/10 "2022-02-01T10:55:25Z")

</div>

OK so `A` is a list of lists (also called a “vector of vectors” in Julia). To start with an empty list of lists, you can write

```julia
A = Vector{Tuple{Int, Int}}[]

```

Here `Vector{Tuple{Int, Int}}` is the _element type_: each element is a vector.

If you don’t know how to write the element type, you can write an example in the REPL and see what Julia prints as type. An example of an element would be `[(1,2)]`:

```julia
julia> [(1,2)]
1-element Vector{Tuple{Int64, Int64}}:
 (1, 2)

```

so the element type is `Vector{Tuple{Int16, Int}16}` (above I used `Int` which is synonymous for `Int64` on most computers).

Now lets push an empty list as first element of `A`:

```julia
julia> push!(A, Tuple{Int, Int}[])

```

And let’s push a tuple in the first list:

```julia
julia> push!(A[1], (1, 2));

julia> A
1-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(1, 2)]

```

Remarks:

- To define `A[5]` you must also define (push or append) `A[1]`, …, `A[4]`. Julia wont create the intermediary values automatically as in Matlab.

- If performance is not a concern, you can use generic vectors (`Vector{Any}`) instead of typed vectors:
