Python filter dictionary by keys

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.

Here is the Syntax of the filter function

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.

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

my_dict = 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

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.

my_dict = 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

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

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

my_dict = 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 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

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

new_Dict = to_dict = <> for key, value in new_Dict.items(): if (key > 2): to_dictPython filter dictionary by keys = value print("Filtered Dictionary",to_dict)

Here is the screenshot of the following given code

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.

my_dict = 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 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.

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

my_dict = 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

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.

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 key based on value

  • Let us see how to filter a key-based value in a Python dictionary.
  • By using Python list comprehension we can easily perform this task.
  • First we have to initializing the Python dictionary and get selective dictionary keys.

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

my_dict = 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

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.

to_dict = sel_val = = 370> print(sel_val)

Here is the screenshot of the following given code

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.

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

new_list = [, , ] 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 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.

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

new_Dict = 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

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.
my_dict = 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

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

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

Источник

Читайте также:  Bad magic number in java
Оцените статью