Python dictionary filter + Examples

In this Python tutorial, we will discuss the Python dictionary filter. Here we will also cover the below examples:

  • Python dictionary filter keys
  • Python dictionary filter by value
  • Python dictionary filter none values
  • Python dictionary filter key based on value
  • Python filter dictionary from list
  • Python dictionary filter lambda
  • Python dict filter empty values

Python dictionary filter

  • Let us see how to filter a Dictionary in Python by using filter() function.
  • This filter() function will filter the elements of the iterable based on some function. So this filter function is used to filter the unwanted elements.

Syntax:

Here is the Syntax of the filter function

filter(function,iterables)

So this filter function will return the element from these iterables for which this function will become true. When this function will return true for any value of this iterable that value will be returned by this filter function.

Example:

Let’s take an example and check how to filter a Python dictionary.

my_dict = {8:'u',4:'t',9:'z',10:'j',5:'k',3:'s'}

new_filt = dict(filter(lambda val: val[0] % 3 == 0, my_dict.items()))
print("Filter dictionary:",new_filt)

Here is the Screenshot of the following given code

Python dictionary filter
Python dictionary filter

Another example to check how to filter a dictionary in Python.

In this example, we have the even and odd number key elements in the dictionary. So if we want to filter only those elements whose values are even. In this case, check the condition if the key is even then add key-value pair to a new dictionary.

Example:

my_dict = {12:'b',14:'d',19:'c',10:'z',15:'p',4:'o'}

Dictionary = dict()

for (new_key, new_value) in my_dict.items():
   
   if new_key % 2 == 0:
       Dictionary[new_key] = new_value
print("Dictionary filter:",Dictionary)

Here is the Screenshot of the following given code

Python dictionary filter items function
Python dictionary filter items function

Read Python Dictionary index

Another example, how to filter a Python dictionary by using a simple loop.

Example:

Let’s take an example and check how to filter a dictionary

my_dict = {3:'b',14:'d',12:'c',19:'z',16:'p',4:'o'}

loo_dict = dict()

for new_ke, new_val in my_dict.items():
    
    if new_ke%2 == 1:
        loo_dict[new_ke] = new_val
print("Filter Dictionary",loo_dict)

In this example first, we will initialize a dictionary and store key-value pairs where the new_ke variable checks the condition if key%2==1).

Here is the Screenshot of the following given code

Python dictionary filter using simple loop
Python dictionary filter using a simple loop

Python dictionary filter keys

  • Let us see how to filter keys in a Python dictionary.
  • By using for loop and dict.items() function to iterate through key-value pairs.
  • In this example use conditional statement to filter keys from dictionary. In this condition if key is bigger than 2 then it filters a new dictionary

Example:

Let’s take an example and check how to filter keys in a dictionary

new_Dict = {2: "p", 16: "q", 17: "r",1:"u"}
to_dict = {}

for key, value in new_Dict.items():
    if (key > 2):


        to_dict[key] = value

print("Filtered Dictionary",to_dict)

Here is the screenshot of the following given code

Python dictionary filter keys
Python dictionary filter keys

Another example to check how to filter keys in a dictionary by using the list comprehension method

In this example create a Python dictionary and assign them odd and even values as an argument. Now check if keys are even which means it is divisible by 2 using dict comprehension.

Example:

my_dict = {4: "John", 16: "George", 17: "Micheal",12:"James"}
filt_Dict = { key:value for (key,value) in my_dict.items() if key % 2 == 0}

print("Filtered key:",filt_Dict)

Here is the Screenshot of the following given code

Python dictionary filter keys comprehension method
Python dictionary filter keys comprehension method

Read Python Dictionary sort

Python dictionary filter none values

  • Let us see how to filter a dictionary with none values in Python.
  • By using for loop method we have to execute a loop for all the keys and check for values if its none value then we have to append it into a list that stores all the keys.

Example:

Let’s take an example and check how to filter a dictionary with none values

my_dict = {'Mangoes' : 4, 'Cherry' : 6, 'Banana' : None,'oranges': None}

output = []
for element in my_dict:
    if my_dict[element] is  None :
        output.append(element)

print("None keys element : ",output)

Here is the Screenshot of the following given code

Python dictionary filter none values
Python dictionary filter none values

How to filter a dictionary with none values

By using the filter and the Python lambda function we can easily check how to filter a dictionary with none values.

Example:

to_dictionary = dict(john = 8, lizeal = None, shane = 4, watson = None,george = 9)

non_val = dict(filter(lambda item: item[1] is None, to_dictionary.items()))
print(non_val)

Here is the Screenshot of the following given code

Python dictionary filter none values by filter method
Python dictionary filter none values by filter method

Read Python dictionary values to list

Python dictionary filter key based on value

Example:

Let’s take an example and check how to filter a key based on the value

my_dict = {"Micheal" : 6, "George" : 8, "Andrew" : 15, "Luis" : 14}

value_list = ['George', 'Luis']

output = [my_dict[x] for x in value_list if x in my_dict]
print ("values based keys : ",output)

Here is the screenshot of the following given code

Python dictionary filter key based on value
Python dictionary filter key based on the value

Another example to check how to filter a key-based value

By using dictionary comprehension we can check how to filter a key-based value in Python.

Example:

to_dict = {'Australia': 220, 'Germany': 480, 'japan': 389, 'Malaysia': 350}

sel_val = {new_key:new_value for (new_key, new_value) in to_dict.items() if new_value >= 370}
print(sel_val)

Here is the screenshot of the following given code

Python dictionary filter key based on value by dict
Python dictionary filter key based on value by dict

Python filter dictionary from list

  • Let us see how to filter a dictionary from list by using list comprehension method in Python.
  • In this example we will create a new list of dictionary and check the condition if the key is available in the list.

Example:

Let’s take an example and check how to filter a dictionary from the list.

new_list = [{'john':7}, {'john':2}, {'john':9}]
def condition(dic):
    
    return dic['john'] > 7
new_filt = [x for x in new_list if condition(x)]
print(new_filt)

Here is the Screenshot of the following given code

Python filter dictionary from list
Python filter dictionary from the list

Read Python Dictionary update with examples

Python dictionary filter lambda

  • Here we can see how to filter a dictionary using lambda function in Python.
  • lambda function are defined without a name, that’s why it is also called as anonymous function.
  • In this example we initialize a dictionary by containing elements whose values are string of length 8.

Example:

Let’s take an example and check how to filter a dictionary using the lambda function.

new_Dict = {2: "Australia", 16: "Germany", 17: "Japan",1:"China"}
my_dict = dict(filter(lambda val: len(val[1]) == 5,new_Dict.items()))

print(my_dict)

Here is the Screenshot of the following given code

Python dictionary filter lambda
Python dictionary filter lambda

This is how to filter a Python dictionary using lambda.

Python dict filter empty values

  • Here we can see how to filter a Python dictionary with empty values.
  • By using list comprehension we can easily check how to filter a dictionary with empty values.

Example:

my_dict = {'Mangoes' : '', 'Cherry' : '', 'Banana' : '','oranges': ''}

output = []
for element in my_dict:
    if my_dict[element] is  None :
        output.append(element)

print("Empty  element : ",output)

Here is the Screenshot of the following given code

Python dict filter empty values
Python dict filter empty values

You may like the following Python tutorials:

In this Python tutorial, we will discuss the Python dictionary filter. Here we will also cover the below examples:

  • Python dictionary filter keys
  • Python dictionary filter by value
  • Python dictionary filter none values
  • Python dictionary filter key based on value
  • Python filter dictionary from list
  • Python dictionary filter lambda
  • Python dict filter empty values