Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting elements of a Python dictionary greater than a certain value

I need to select elements of a dictionary of a certain value or greater. I am aware of how to do this with lists, Return list of items in list greater than some value.

But I am not sure how to translate that into something functional for a dictionary. I managed to get the tags that correspond (I think) to values greater than or equal to a number, but using the following gives only the tags:

[i for i in dict if dict.values() >= x] 
like image 768
Jesse O Avatar asked Sep 14 '13 22:09

Jesse O


People also ask

How do you select a specific value in a dictionary Python?

In Python, you can get the value from a dictionary by specifying the key like dict[key] . In this case, KeyError is raised if the key does not exist. Note that it is no problem to specify a non-existent key if you want to add a new element.

How do you find the greatest value in a dictionary?

By using max() and dict. get() method we can easily get the Key with maximum value in a dictionary. To obtain the maximum value from the dictionary we can use the in-built max() function. In this example, we can use iterable and dict to get the key paired with the maximum value.

How do you filter a dictionary based on values?

Filter a Dictionary by values in Python Let's use the same filterTheDict() function created above to filter the dictionary. Suppose we want to keep the elements only in dictionary whose value field contains a string of length 6. To do that let's pass the different lambda function to filterTheDict() i.e.

What does .items do in Python?

The items() method returns a view object that displays a list of dictionary's (key, value) tuple pairs.


Video Answer


2 Answers

.items() will return (key, value) pairs that you can use to reconstruct a filtered dict using a list comprehension that is feed into the dict() constructor, that will accept an iterable of (key, value) tuples aka. our list comprehension:

>>> d = dict(a=1, b=10, c=30, d=2) >>> d {'a': 1, 'c': 30, 'b': 10, 'd': 2} >>> d = dict((k, v) for k, v in d.items() if v >= 10) >>> d {'c': 30, 'b': 10} 

If you don't care about running your code on python older than version 2.7, see @opatut answer using "dict comprehensions":

{k:v for (k,v) in dict.items() if v > something} 
like image 142
nmaier Avatar answered Oct 01 '22 13:10

nmaier


While nmaier's solution would have been my way to go, notice that since python 2.7+ there has been a "dict comprehension" syntax:

{k:v for (k,v) in dict.items() if v > something} 

Found here: Create a dictionary with list comprehension in Python. I found this by googling "python dictionary list comprehension", top post.

Explanation

  • { .... } includes the dict comprehension
  • k:v what elements to add to the dict
  • for (k,v) in dict.items() this iterates over all tuples (key-value-pairs) of the dict
  • if v > something a condition that has to apply on every value that is to be included
like image 33
opatut Avatar answered Oct 01 '22 12:10

opatut