How to extract first arguments from dictionaries in Julia?

Hello,
this is a basic question but I am not familiar with dictionaries, preferring arrays. I have set a dictionary as

julia> D = Dict("Field_1"=>[1,2,3,4,5], 
           "Field_2"=>["a", "b", "c", "d", "e"])
Dict{String, Vector} with 2 entries:
  "Field_1" => [1, 2, 3, 4, 5]
  "Field_2" => ["a", "b", "c", "d", "e"]

What is the function to assess how many entries are in it? That is, I should get a n = 5 somehow?
Also, it is possible to extract the first entry of each field and assign it to a different array?
That is, if I reiterate between 1 and n, can I do something like:

d = ["Field_1" [1], "Field_2" [a]]
A = d[1]
B = d[2]

Thank you

By β€œit” do you mean the arrays stored as the values in the dictionary? A dictionary is made up of (key, value) pairs:

julia> for (k, v) ∈ pairs(D)
           println("Key: ", k, " value: ", v)
       end
Key: Field_1 value: [1, 2, 3, 4, 5]
Key: Field_2 value: ["a", "b", "c", "d", "e"]

So if you want to check the number of entries in each array you can do:

julia> length.(values(D))
2-element Vector{Int64}:
 5
 5

I’m afraid I don’t understand the second question. The first element of each array in your dict is accessed in the same way as computing the length above:

julia> first.(values(D))
2-element Vector{Any}:
 1
  "a"

You could use, e.g., length(D["Field_1"]) to obtain the length of the array in Field_1, and first(D["Field_1"]) to obtain the first element.

1 Like

Thank you. k and v extract the data exactly as needed. length gives me also the information I needed.