119

I have a dictionary in Python, and what I want to do is get some values from it as a list, but I don't know if this is supported by the implementation.

myDictionary.get('firstKey')   # works fine

myDictionary.get('firstKey','secondKey')
# gives me a KeyError -> OK, get is not defined for multiple keys
myDictionary['firstKey','secondKey']   # doesn't work either

Is there any way I can achieve this? In my example it looks easy, but let's say I have a dictionary of 20 entries, and I want to get 5 keys. Is there any other way than doing the following?

myDictionary.get('firstKey')
myDictionary.get('secondKey')
myDictionary.get('thirdKey')
myDictionary.get('fourthKey')
myDictionary.get('fifthKey')
wovano
  • 3,264
  • 4
  • 18
  • 43
PKlumpp
  • 4,485
  • 7
  • 34
  • 60
  • 1
    Possible duplicate of [Python dictionary: Get list of values for list of keys](https://stackoverflow.com/questions/18453566/python-dictionary-get-list-of-values-for-list-of-keys) – Jundiaius May 08 '18 at 12:56
  • 1
    Super hard question to search for. Glad I finally found this! – Stephen Sep 16 '19 at 15:09

11 Answers11

131

There already exists a function for this:

from operator import itemgetter

my_dict = {x: x**2 for x in range(10)}

itemgetter(1, 3, 2, 5)(my_dict)
#>>> (1, 9, 4, 25)

itemgetter will return a tuple if more than one argument is passed. To pass a list to itemgetter, use

itemgetter(*wanted_keys)(my_dict)

Keep in mind that itemgetter does not wrap its output in a tuple when only one key is requested, and does not support zero keys being requested.

Veedrac
  • 54,508
  • 14
  • 106
  • 164
  • this is cool. it's not as short as I expected, but nice to know ;) – PKlumpp Jun 13 '14 at 12:05
  • 3
    How much shorter can you get? If the name is too long... `g = itemgetter`. o.O – Veedrac Jun 13 '14 at 12:10
  • I expected it to have some built in syntax that I couldn't find. of course you can make it shorter your way, but in any code review I would be asked: wtf is g(mykeys)(myDict) :D – PKlumpp Jun 13 '14 at 12:16
  • 1
    Is there an option to return a dictionary of key: value pairs for specified list of keys rather than just the values? – alancalvitti Dec 26 '18 at 19:57
  • @alancalvitti I don't think so. – Veedrac Dec 26 '18 at 20:40
  • 1
    @alancalvitti you can always do `{key: mydictionary.get(key) for key in keys}` – Buggy Oct 17 '19 at 12:59
  • 3
    Small catch with this, `itemgetter` requires at least one argument, so if you were looking to do something like `itemgetter(*some_keys)(some_dict)`, it will fail if `some_keys` is an empty list. – Christian Reall-Fluharty Nov 16 '19 at 00:42
  • 2
    itemgetter will not deal with KeyError – NeilG Feb 11 '20 at 02:58
  • If you want to extract the values into multiple variables and not to a tuple you can mix @Veedrac and @Epion solutions: `first_val, second_val = itemgetter(1, 3)(my_dict)` – ET-CS Jul 21 '20 at 13:50
  • Awesome solution, but definitely a case for a new language feature: one that could take an indefinite number of values 0-n, always returns some type of Sequence, allows for specific fields, and reliably returns the same type. – Joe Sadoski Oct 04 '21 at 14:30
91

Use a for loop:

keys = ['firstKey', 'secondKey', 'thirdKey']
for key in keys:
    myDictionary.get(key)

or a list comprehension:

[myDictionary.get(key) for key in keys]
ComputerFellow
  • 10,784
  • 11
  • 48
  • 60
  • 2
    ok I already thought about this, but is there really no implemented way? I mean, this is not something completely odd I guess – PKlumpp Jun 13 '14 at 11:21
  • 3
    If it is guaranteed that every key in keys is in the dictionary, you can write `[myDictionary[key] for key in keys]`, but it does not get much simpler than that. – timgeb Jun 13 '14 at 11:34
  • 4
    or `[my_dict.get(x) for x in ['firstKey', 'secondKey', 'thirdKey']]`for a single line solution but that is as compact as you will get. – Padraic Cunningham Jun 13 '14 at 11:34
  • Depending on the use-case you might want an `if` statement to check for `None` values returned by the `get` method: `[mydict.get(key) for key in keys if key in mydict]` – Carolyn Conway Feb 08 '17 at 22:54
  • 1
    @timgeb comment is True, and it is faster. – Muhammad Yasirroni Mar 17 '22 at 08:58
45

I'd suggest the very useful map function, which allows a function to operate element-wise on a list:

mydictionary = {'a': 'apple', 'b': 'bear', 'c': 'castle'}
keys = ['b', 'c']

values = list( map(mydictionary.get, keys) )

# values = ['bear', 'castle']
scottt
  • 609
  • 5
  • 6
  • 3
    One amendment to get this to work, you have to encase `map` with a list: `values = list( map(mydictionary.get, keys) )` – kilozulu Jan 28 '20 at 21:19
  • And this _does_ deal with KeyError – NeilG Feb 11 '20 at 03:04
  • I timed this solution compared to a list comprehension (which would have otherwise been my preference) and this solution without encasing in a list is 3.5x faster so (+1) from me for the cases when we can use the iterator directly. (Encasing in a list makes it take just as long and for that case I would just do the list comprehension which seems easier to read). – Dan Boschen Jan 23 '22 at 18:17
10

You can use At from pydash:

from pydash import at
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_list = at(my_dict, 'a', 'b')
my_list == [1, 2]
bustawin
  • 605
  • 7
  • 10
7

As I see no similar answer here - it is worth pointing out that with the usage of a (list / generator) comprehension, you can unpack those multiple values and assign them to multiple variables in a single line of code:

first_val, second_val = (myDict.get(key) for key in [first_key, second_key])
Epion
  • 438
  • 3
  • 7
5

If you have pandas installed you can turn it into a series with the keys as the index. So something like

import pandas as pd

s = pd.Series(my_dict)

s[['key1', 'key3', 'key2']]
Mosqueteiro
  • 99
  • 1
  • 4
4

I think list comprehension is one of the cleanest ways that doesn't need any additional imports:

>>> d={"foo": 1, "bar": 2, "baz": 3}
>>> a = [d.get(k) for k in ["foo", "bar", "baz"]]
>>> a
[1, 2, 3]

Or if you want the values as individual variables then use multiple-assignment:

>>> a,b,c = [d.get(k) for k in ["foo", "bar", "baz"]]
>>> a,b,c
(1, 2, 3)
Andy Brown
  • 10,341
  • 2
  • 35
  • 54
1
def get_all_values(nested_dictionary):
    for key, value in nested_dictionary.items():
        if type(value) is dict:
            get_all_values(value)
        else:
            print(key, ":", value)

nested_dictionary = {'ResponseCode': 200, 'Data': {'256': {'StartDate': '2022-02-07', 'EndDate': '2022-02-27', 'IsStoreClose': False, 'StoreTypeMsg': 'Manual Processing Stopped', 'is_sync': False}}}

get_all_values(nested_dictionary)
0

TLDR: Use (dict1.get(key) for key in keys)


Use tuple (not list) comprehension because it is the fastest way. .get(key) and [key] have comparable speed, sometimes [key] is a little bit faster. If no key found in dict, .get(key) is safer (return None) than directly using indexing [key] (throw Error).

Benchmark:

from fakertype import FakerType
from faker import Faker

Faker.seed(42)
ft = FakerType()
dict1 = ft.fake_dict(n=100)
keys = list(dict1.keys())[:50]

%%timeit
[dict1[key] for key in keys]
# 14 µs ± 4.22 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%%timeit
itemgetter(*keys)(dict1)
# 2.8 µs ± 596 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%%timeit
(dict1.get(key) for key in keys)
# 585 ns ± 33 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit
(dict1[key] for key in keys)
# 590 ns ± 25.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
-1

If the fallback keys are not too many you can do something like this

value = my_dict.get('first_key') or my_dict.get('second_key')
Mux
  • 19
  • 2
-2
def get_all_values(nested_dictionary):
    for key, val in nested_dictionary.items():
        data_list = []
        if type(val) is dict:
            for key1, val1 in val.items():
                data_list.append(val1)

    return data_list