Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an array of all keys in a Dictionary?

How can one get all the keys of a dictionary as a separate array in Julia.

For example:

Dict("a" => 123, "b" => 456, "c" => 789)

would give the following Array:

["a", "b", "c"]
like image 316
Wali Waqar Avatar asked Nov 15 '25 07:11

Wali Waqar


1 Answers

Here is how you can do it (if an iterator is enough for you just use keys to avoid materializing the array):

julia> d = Dict("a" => 123, "b" => 456, "c" => 789)
Dict{String, Int64} with 3 entries:
  "c" => 789
  "b" => 456
  "a" => 123

julia> keys(d)
KeySet for a Dict{String, Int64} with 3 entries. Keys:
  "c"
  "b"
  "a"

julia> collect(keys(d))
3-element Vector{String}:
 "c"
 "b"
 "a"
like image 86
Bogumił Kamiński Avatar answered Nov 17 '25 19:11

Bogumił Kamiński