How to get values of dictionary by using array of keys?

That’s nice! :grinning: The list comprehension is very natural in Python though, which can bias one’s appreciation.

Of dubious interest, but possibly worthy of note:

using BenchmarkTools
const dict = Dict(i => j for (j,i) in enumerate('A':'Z'))
const keys = collect('B':'Y')

@benchmark get.(Ref(dict), keys, missing)
BenchmarkTools.Trial: 10000 samples with 590 evaluations.
 Range (min … max):  200.120 ns …  1.306 ΞΌs  β”Š GC (min … max): 0.00% … 82.69%
 Time  (median):     210.534 ns              β”Š GC (median):    0.00%
 Time  (mean Β± Οƒ):   219.662 ns Β± 55.169 ns  β”Š GC (mean Β± Οƒ):  1.39% Β±  4.74%

   β–ƒβ–‡β–‡β–ˆβ–‡β–†β–†β–†β–…β–„β–ƒβ–ƒβ–ƒβ–‚β–‚β–‚β–ƒβ–‚β–                                         β–‚
  β–‡β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‡β–ˆβ–‡β–‡β–‡β–‡β–…β–‡β–‡β–‡β–‡β–†β–‡β–†β–…β–‡β–…β–…β–…β–†β–…β–…β–„β–…β–…β–…β–‡β–†β–†β–†β–…β–„β–…β–„ β–ˆ
  200 ns        Histogram: log(frequency) by time       313 ns <

 Memory estimate: 280 bytes, allocs estimate: 3.

@benchmark [haskey(dict, k) ? dict[k] : missing for k in keys] 
BenchmarkTools.Trial: 10000 samples with 257 evaluations.
 Range (min … max):  299.370 ns …   3.284 ΞΌs  β”Š GC (min … max): 0.00% … 89.86%
 Time  (median):     307.296 ns               β”Š GC (median):    0.00%
 Time  (mean Β± Οƒ):   323.600 ns Β± 101.617 ns  β”Š GC (mean Β± Οƒ):  1.00% Β±  3.26%

  β–ˆβ–…β–†β–„β–„β–ƒβ– ▁▄▂▁▁                                                 ▁
  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‡β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‡β–‡β–‡β–†β–†β–‡β–†β–‡β–ˆβ–‡β–†β–…β–…β–†β–…β–†β–†β–†β–†β–†β–†β–…β–…β–ƒβ–†β–†β–…β–…β–…β–…β–…β–„β–ƒβ–ƒβ–‚β–„β–„β–„β–‚β–„β–„β–„β–„β–ƒβ–„β–„ β–ˆ
  299 ns        Histogram: log(frequency) by time        555 ns <

 Memory estimate: 256 bytes, allocs estimate: 1.
3 Likes