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 ??
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]
2element Vector{Pair{Int64, Int64}}:
1 => 2
1 => 5
julia> append!(x, [3 => 4, 2 =>5])
4element Vector{Pair{Int64, Int64}}:
1 => 2
1 => 5
3 => 4
2 => 5
Maybe @ahmad88 was using âpairâ in the general/mathematical sense of a twoelement tuple. Using append!
as suggested by @nilshg also works in that case:
julia> A = [(1,2), (1,5), (2,4)]
3element Vector{Tuple{Int64, Int64}}:
(1, 2)
(1, 5)
(2, 4)
julia> append!(A, [(3,4), (2,5), (3,6)])
6element 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 ith element:
julia> A[3]
(2, 4)
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
1element 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
2element 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
3element 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[1]=
then
append!(A[1], [(3,4), (2,5), (3,6)])
or
push!(A[1], (4,7))
is it possible like this ??
Both of these just work?
julia> append!(A[1], [(3, 4), (2, 5), (3, 6)])
6element Vector{Tuple{Int64, Int64}}:
(1, 2)
(1, 5)
(2, 4)
(3, 4)
(2, 5)
(3, 6)
julia> push!(A[1], (4, 7))
7element 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))
1element 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[5], (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]
10element Vector{Vector{Tuple{Int64, Int64}}}:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
julia> push!(A[5], (2, 3))
1element Vector{Tuple{Int64, Int64}}:
(2, 3)
julia> A
10element Vector{Vector{Tuple{Int64, Int64}}}:
[]
[]
[]
[]
[(2, 3)]
[]
[]
[]
[]
[]
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)]
1element 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], (1, 2));
julia> A
1element 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:julia> A = [] Any[] julia> push!(A, []); julia> push!(A[1], (1, 2)); julia> A 1element Vector{Any}: Any[(1, 2)]