python dictionary: How to get all keys with specific values


Is it possible to get all keys in a dictionary with values above a threshold?

A dictionary could look like:

mydict = {(0,1,2): "16", (2,3,4): "19"}

The threshold could be 17 for example.

Asked By: Varlor



Of course it is possible. We can simply write:

[k for k,v in mydict.items() if float(v) >= 17]

Or in the case you work with , you – like @NoticeMeSenpai says – better use:

[k for k,v in mydict.iteritems() if float(v) >= 17]

This is a list comprehension. We iterate through the key-value pairs in the mydict dictionary. Next we convert the value v into a float(v) and check if that float is greater than or equal to 17. If that is the case, we add the key k to the list.

For your given mydict, this generates:

>>> [k for k,v in mydict.items() if float(v) >= 17]
[(2, 3, 4)]

So a list containing the single key that satisfied the condition here: (2,3,4).

Answered By: Willem Van Onsem

One can use filter() function here.

list(filter(lambda k: float(mydict[k]) >= 17, mydict))

Another even more convoluted way is to create boolean selectors and compress similar to how boolean indexing works in pandas/numpy.

# using built-in methods
from itertools import compress, repeat
import operator
list(compress(mydict, map(, map(float, mydict.values()), repeat(17))))
# or using a lambda
list(compress(mydict, map(lambda x: float(x) >= 17, mydict.values())))

Speaking of pandas/numpy, if you need to filter keys by values repeatedly, pandas Series or numpy array is a useful data type to store the data in, so that this kind of filtering can be done vectorially in a more efficient way.

Using pandas Series:

import pandas as pd
my_srs = pd.Series(mydict).astype(int)
my_srs.index[my_srs >= 17].tolist()

Using numpy arrays (will have to create two arrays, one for the keys and another for the values and use the values array to filter the keys array):

import numpy as np
keys = np.array(list(mydict.keys()), dtype=object)
values = np.array(list(mydict.values()), dtype=float)
keys[values >= 17].tolist()

Using numpy structured array:

import numpy as np
arr = np.array(list(mydict.items()), dtype=[('keys', object), ('values', int)])
arr['keys'][arr['values'] >= 17]
Answered By: cottontail
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.