Here's an alternative approach with map:
dictionary = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
keys = [['a','b'], ['c','d','e']]
items = [list(map(dictionary.get, k)) for k in keys]
Conclusion:
[[1, 2], [3, 4, 5]]
One huge plus is a much more reliable way to handle non-existent keys (thanks, @ Ev.Kounis!).
Alternative ft. Ev Kounis, including returning the default value to no-key:
items = [[dictionary.get(x, 0) for x in sub] for sub in keys]
source
share