Keys and values – looping

To isolate the keys of a dictionary, use the keys function ki = keys(d3), with ki being a KeyIterator object, which we can use in a for loop as follows:

for k in keys(d3) 
    println(k) 
end 

Assuming d3 is again d3 = Dict(:A => 100, :B => 200), this prints out A and B. This also gives us an alternative way to test if a key exists with in. For example, :A in keys(d3) returns true and :Z in keys(d3) returns false.

If you want to work with an array of keys, use collect(keys(d3)), which returns a two-element Array{Symbol,1} that contains :A and :B. To obtain the values, use the values function: vi = values(d3), with vi being a ValueIterator object, which we can also loop through with for:

for v in values(d3) println(v) end ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.