Home > Software engineering >  How to iterate over Dict in Julia
How to iterate over Dict in Julia

Time:01-06

How can I something like this (it is in Python) in Julia?

for key, value in dictionary.items(): # <-- I don't know how to iterate over keys and values of a dictionary in Julia
    print(key, value)

Thank you.

CodePudding user response:

You can just iterate, but you need a bracket around (key, value):

julia> dict = Dict(i => (i/10   rand(1:99)) for i = 1:3)
Dict{Int64, Float64} with 3 entries:
  2 => 24.2
  3 => 29.3
  1 => 41.1

julia> for (k,v) in dict
         @show k v
       end
k = 2
v = 24.2
k = 3
v = 29.3
k = 1
v = 41.1

julia> p = first(dict)
2 => 24.2

julia> typeof(p)
Pair{Int64, Float64}

julia> (a, b) = p;

julia> b
24.2
  •  Tags:  
  • Related