In this article we will discuss different ways to filter contents from a dictionary by conditions on keys or value or on both.

Suppose we have a dictionary in which int type element is key and string type elements are value i.e.

Now let’s discuss different ways to filter the contents of this dictionary arbitrary conditions,

Filter a Dictionary by conditions by creating a Generic function

Filter a Dictionary by keys in Python

Suppose we want to filter above dictionary by keeping only elements whose keys are even. For that we can just iterate over all the items of
dictionary and add elements with even key to an another dictionary i.e.

Output:

The filtered dictionary i.e. newDict now contains filtered elements from the original dictionary i.e. elements whose key is divisible by 2. Similarly we can have conditional filtering based on value field instead of key. But instead of writing code for iteration and condition checking again and again, we move the code to a generic function and pass condition from outside i.e.

This function accepts,

  • A dictionary
  • A function that accepts a key/value pair and returns True or False

This function iterate over all the key value pairs in dictionary and call the given callback function() on each pair. Items for which callback() function returns True are added to the new dictionary. In the end new dictionary is returned.

Now let’s use this to filter a dictionary by key i.e.

Output:

Passed lambda function is used as condition while filtering contents inside function filterTheDict(). It returned a new dictionary which contains elements with even keys only.

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.

Output:

Passed lambda function to filterTheDict() in which we are checking condition on value field only. It returned a new dictionary which contains elements whose value is string of length 6.

Filter a Dictionary by filter()

Instead of creating our own function we can also use python’s filter() function too.

filter() function accepts a,

  • an iterable sequence to be filtered
  • a function that accepts an argument and returns bool i.e. True or False based on it’s logic

Returns:

  • A new sequence of filtered contents.

We need to convert this returned sequence to dict again. Let’s use filter() to do conditional filtering on dictionary.
Our original dictionary is,

Filter a Dictionary by keys in Python using filter()

Let’s filter items in dictionary whose keys are even i.e. divisible by 2,

Output:

Filter a Dictionary by values in Python using filter()

Let’s filter items in dictionary whose values are string of length 6,

Output:

filter() function iterates above all the elements in passed dict and filter elements based on condition passed as callback.

Filter a Dictionary by Dict Comprehension

Our original dictionary is,

Filter a Dictionary by keys in Python using dict comprehension

Let’s filter items in dictionary whose keys are even i.e. divisible by 2 using dict comprehension ,

Output:

Filter a Dictionary by values in Python using dict comprehension

Let’s filter items in dictionary whose values are string of length 6 by using dict comprehension,

Output:

It iterates above all the elements in passed dict and filter elements based on condition passed as callback.

Learn more about Dictionaries in Python,

Complete example is as follows,

Output:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,