How to Filter a Dictionary According to an Arbitrary Condition Function

How to filter a dictionary according to an arbitrary condition function?

You can use a dict comprehension:

{k: v for k, v in points.items() if v[0] < 5 and v[1] < 5}

And in Python 2, starting from 2.7:

{k: v for k, v in points.iteritems() if v[0] < 5 and v[1] < 5}

filter items in a python dictionary where keys contain a specific string

How about a dict comprehension:

filtered_dict = {k:v for k,v in d.iteritems() if filter_string in k}

One you see it, it should be self-explanatory, as it reads like English pretty well.

This syntax requires Python 2.7 or greater.

In Python 3, there is only dict.items(), not iteritems() so you would use:

filtered_dict = {k:v for (k,v) in d.items() if filter_string in k}

The best way to filter a dictionary in Python

d = dict((k, v) for k, v in d.iteritems() if v > 0)

In Python 2.7 and up, there's nicer syntax for this:

d = {k: v for k, v in d.items() if v > 0}

Note that this is not strictly a filter because it does create a new dictionary.

Filter values of dictionary

You can use dictionary comprehension, to create a dictionary with the filtered values, like this

>>> {key: d1[key] for key in d1 if d1[key] > 0}
{'c': 8, 'd': 7}

You can also use dict.items() like this

>>> {key: value for key, value in d1.items() if value > 0}
{'c': 8, 'd': 7}

Note: Using dict.iteritems() will be more memory efficient in Python 2.7, as dict.items() will create a list of tuples with all the key-value pairs.

If your intention is to just remove the values which are 0, then you can even shorten it like this

>>> {key: value for key, value in d1.items() if value}
{'c': 8, 'd': 7}

How to to filter dict to select only keys greater than a value?

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

Filter dict to contain only certain keys?

Constructing a new dict:

dict_you_want = { your_key: old_dict[your_key] for your_key in your_keys }

Uses dictionary comprehension.

If you use a version which lacks them (ie Python 2.6 and earlier), make it dict((your_key, old_dict[your_key]) for ...). It's the same, though uglier.

Note that this, unlike jnnnnn's version, has stable performance (depends only on number of your_keys) for old_dicts of any size. Both in terms of speed and memory. Since this is a generator expression, it processes one item at a time, and it doesn't looks through all items of old_dict.

Removing everything in-place:

unwanted = set(keys) - set(your_dict)
for unwanted_key in unwanted: del your_dict[unwanted_key]

How to filter dictionary by value?

You can use a dictionary comprehension with unpacking for a more Pythonic result:

d=[{'fpdownload2.macromedia.com': (1, 88),
'laposte.net': (2, 23),
'www.laposte.net': (3, 119),
'www.google.com': (4, 5441),
'match.rtbidder.net': (5, 84),
'x2.vindicosuite.com': (6, 37),
'rp.gwallet.com': (7, 88)}]
new_data = [{a:(b, c) for a, (b, c) in d[0].items() if c < 100}]

Output:

[{'laposte.net': (2, 23), 'fpdownload2.macromedia.com': (1, 88), 'match.rtbidder.net': (5, 84), 'x2.vindicosuite.com': (6, 37), 'rp.gwallet.com': (7, 88)}]

Filter dict in python?

You can try .items() like this:

my_dict = {
"Value1": "2000",
"Value2": "3000",
"Value3": "4000"
}

for (key,value) in my_dict.items():
if (int(value) > 2000):
print(value)


Related Topics



Leave a reply



Submit