# List of pairs

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

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

``````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> 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
``````
1 Like

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> 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!`:

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

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

``````julia> A
(2, 4)
``````
1 Like

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

Okay, thanks for clarifying. @sudete 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> 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 # each element in A is now a vector of tuples
3-element Vector{Tuple{Int64, Int64}}:
(3, 4)
(2, 5)
(3, 6)
``````

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

Both of these just work?

``````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)

julia> push!(A, (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> x = Tuple{Int64, Int64}[]
Tuple{Int64, Int64}[]

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

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

``````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, (2, 3))`
so (2,3) will be the first pair in the 5th list

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

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

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

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

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

``````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> [(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> push!(A, Tuple{Int, Int}[])
``````

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

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

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

Remarks:

• To define `A` you must also define (push or append) `A`, …, `A`. 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:

``````julia> A = []
Any[]

julia> push!(A, []);

julia> push!(A, (1, 2));

julia> A
1-element Vector{Any}:
Any[(1, 2)]
``````
1 Like