When I use keys()
the resulting data structure is a Base.KeySet()
. Can someone elaborate on what that is? I searched the documentation, but I couldn’t find it.
character_inventory = Dict{String,Int32}("Sword" => 100, "Staff" => 50)
character_inventory_keys = keys(character_inventory)
println(typeof(character_inventory_keys))
Output:
Base.KeySet{String,Dict{String,Int32}}
1 Like
The documentation for keys
describe it a bit although it doesn’t mention KeySet
explicitly:
help?> keys
keys(a::AbstractDict)
Return an iterator over all keys in a dictionary. [...]
And to be more explicit, KeySet
is one such iterator which yields the keys of the dictionary when iterated. It is simply a “lazy-view” of the keys (see https://github.com/JuliaLang/julia/blob/10b0a014275ad4836fad8d2a2590d457dc2286ad/base/abstractdict.jl#L39-L41) and it is implemented like this (presumably) for efficiency since you don’t have to materialize an array or similar with all the keys.
4 Likes
I think you meant ‘use keys()
without collect()
’, if you use collect
you get an Array
as expected.