3 Ways to Filter List of Dictionaries Based on Key Values

Filter List Of Dictionaries Based On Key Value

To filter a list of dictionaries, we need to have a condition that needs to be satisfied by the keys of the dictionary to create a new filtered dictionary.

A dictionary is the most popularly used data storage structure of Python. It stores the data in the form of a key-value pair, where the values can be accessed with the help of the key.

Since data is stored in keys and values, data can be modified, updated, and deleted using Python’s built-in functions.

One thing to remember while working with dictionaries is that no key should be duplicated. Because a dictionary maps a particular key with some corresponding value and logically, a key being mapped to more than one value seems odd.

Coming to the manipulation of the dictionary, we can filter a list of dictionaries based on some condition and return another dictionary that satisfies the condition.

Related: Read this article to know how to get the key with maximum value in the dictionary.

What Is a Dictionary?

A dictionary is an unordered collection of items that stores data in key: value fashion. By unordered, we mean that the items need not follow any order while initialization but can be sorted later.

Note:

In the the older versions of python(<= 3.6), the dictionaries were unordered. But in the later versions(>=3.7), dictionaries need to be ordered.

The dictionary’s items are enclosed in curly braces and are mutable. The mutable data type is a data type that can be changed after its declaration.

Visit this post to learn more about the immutable data types of Python.

Here is an example dictionary and some operations performed on the dictionary.

dictn ={'Name':['Ajay','Bindhu','Chandan','Dikshit','Akarsh','Fathima'],
        'Age':[12,14,12,13,14,15],
        'Grade':[7,8,7,8,9,10],
        'Marks':[400,450,347,560,456,458]}
print(dictn)

The dictionary we created is called dictn and has four keys- Name, Age, Grade, and Marks. These keys have some values associated with them. This dictionary is printed in the last line using the print function.

Here is the output.

{'Name': ['Ajay', 'Bindhu', 'Chandan', 'Dikshit', 'Akarsh', 'Fathima'], 'Age': [12, 14, 12, 13, 14, 15], 'Grade': [7, 8, 7, 8, 9, 10], 'Marks': [400, 450, 347, 560, 456, 458]}

We can also print the keys and values of the dictionaries separately.

ks=dictn.keys()
print(ks)

The keys function is used to print the keys of the dictionary.

dict_keys(['Name', 'Age', 'Grade', 'Marks'])

Similarly, the values function is used to print the values of the dictionary.

vs=dictn.values()
print(vs)

This code outputs a list of values of the dictionary.

dict_values([['Ajay', 'Bindhu', 'Chandan', 'Dikshit', 'Akarsh', 'Fathima'], [12, 14, 12, 13, 14, 15], [7, 8, 7, 8, 9, 10], [400, 450, 347, 560, 456, 458]])

We can even delete a certain key from the dictionary with the help of del keyword.

del dictn['Grade']
print("The new dictionary is :",dictn)

In the above code snippet, we are trying to remove the Grade key, and the resultant dictionary is printed in the next line.

The new dictionary is : {'Name': ['Ajay', 'Bindhu', 'Chandan', 'Dikshit', 'Akarsh', 'Fathima'], 'Age': [12, 14, 12, 13, 14, 15], 'Marks': [400, 450, 347, 560, 456, 458]}

Let us try to remove the last key: value pair of the dictionary using the popitem() method. Since this method just removes the last item of the dictionary, it does not accept any input from the user.

newdictn=dictn.popitem()
print("The new dictionary is", dictn)

This results in the following output.

The new dictionary is {'Name': ['Ajay', 'Bindhu', 'Chandan', 'Dikshit', 'Akarsh', 'Fathima'], 'Age': [12, 14, 12, 13, 14, 15]}

The last key: the value pair of the dictionary was Marks. So the popitem() removed this pair and so the output does not have the item.

Here is the complete code and output.

Dictionary
Dictionary

How to Filter List of Dictionaries Based on Key Values?

To filter a list of dictionaries, we have four approaches. We are going to take the same dictionary(dictn) and filter it based on some pre-defined conditions.

Before we move on with the methods, let us first see the list of dictionaries.

The list of dictionaries should follow the below syntax.

dict={['Key1':Value,'Key2':Value......},{'Key1':Value1,'Key2':Value2.......},]

The list of dictionaries we are going to use is given below.

dictn =[{'Name':'Ajay','Age':12,'Grade':7,'Marks':400},
        {'Name':'Bindhu','Age':14,'Grade':8,'Marks':450},
        {'Name':'Chandan','Age':12,'Grade':7,'Marks':347},
        {'Name':'Dikshit','Age':13,'Grade':8,'Marks':560},
        {'Name':'Akarsh','Age':14,'Grade':9,'Marks':456},
        {'Name':'Fathima','Age':15,'Grade':10,'Marks':458},
]
print("The list of dictionaries is:")
print(dictn)

We just took the original dictionary and split it into smaller dictionaries belonging to different students and stored them in the form of a list.

The output is given below.

The list of dictionaries is:
[{'Name': 'Ajay', 'Age': 12, 'Grade': 7, 'Marks': 400}, {'Name': 'Bindhu', 'Age': 14, 'Grade': 8, 'Marks': 450}, {'Name': 'Chandan', 'Age': 12, 'Grade': 7, 'Marks': 347}, {'Name': 'Dikshit', 'Age': 13, 'Grade': 8, 'Marks': 560}, {'Name': 'Akarsh', 'Age': 14, 'Grade': 9, 'Marks': 456}, {'Name': 'Fathima', 'Age': 15, 'Grade': 10, 'Marks': 458}]

Filter List of Dictionaries Using Comprehension

We are going to use list comprehension to filter the above list of dictionaries. List comprehension is used to create a new list based on some criteria from an already existing list in shorter code.

aget = 12
newl = [d for d in dictn if d['Age'] > aget]
filtered_dict = dict((d['Name'], d) for d in newl)
print("The filtered dictionary as a dictionary is:")
print(filtered_dict)

In the first line, we are initializing a variable called aget that stores the threshold age value. This value is used to filter the list of dictionaries.

A variable called newl is created to perform the list comprehension. In this process, we are creating a variable called d to go through every age item in the dictionary to check if it is greater than the threshold(12).

This list is then converted to a dictionary using the dict() method. Lastly, the filtered dictionary is printed.

Filter List Of Dictionaries Using Comprehension
Filter List Of Dictionaries Using Comprehension

Here is the complete output.

The filtered dictionary as a dictionary is:
{'Bindhu': {'Name': 'Bindhu', 'Age': 14, 'Grade': 8, 'Marks': 450}, 'Dikshit': {'Name': 'Dikshit', 'Age': 13, 'Grade': 8, 'Marks': 560}, 'Akarsh': {'Name': 'Akarsh', 'Age': 14, 'Grade': 9, 'Marks': 456}, 'Fathima': {'Name': 'Fathima', 'Age': 15, 'Grade': 10, 'Marks': 458}}

Filter List of Dictionaries Using Filter()

The filter is a built-in function of the Python language that takes an iterable object(list, tuple, dictionary) and filters it based on the condition provided.

aget = 12
filteredlist = list(filter(lambda d: d['Age'] > aget,dictn))
newdict = dict((d['Name'], d) for d in filteredlist)
print("The filtered dictionary using lambda is:")
print(newdict)

We are using the same condition as in the previous example that is the age of the students should be greater than 12. A new list called filteredlist is created to store the list obtained from filtering the dictionary using the filter method. The lambda function is used to check through every age item in the dictionary and verify if it is greater than the threshold.

The filtere3dlist is converted to a dictionary using the dict method and is stored in a variable called newdict. This filtered list is printed in the next line.

Filter List Of Dictionaries Using Filter()
Filter List Of Dictionaries Using Filter()

Here is the complete output.

The filtered dictionary using lambda is:
{'Bindhu': {'Name': 'Bindhu', 'Age': 14, 'Grade': 8, 'Marks': 450}, 'Dikshit': {'Name': 'Dikshit', 'Age': 13, 'Grade': 8, 'Marks': 560}, 'Akarsh': {'Name': 'Akarsh', 'Age': 14, 'Grade': 9, 'Marks': 456}, 'Fathima': {'Name': 'Fathima', 'Age': 15, 'Grade': 10, 'Marks': 458}}

Filter List of Dictionaries Using For Loop

We can also use a for loop to check if the items in the dictionary satisfy a given condition.

mr=350
nlist = []
for d in dictn:
    if d['Marks'] > mr:
        nlist.append(d)
print("The filtered list is")
print(nlist)
ndict= dict((d['Name'], d) for d in nlist)
print("The filtered dictionary using for loop is:")
print(ndict)

In this example, we are going for a different condition. The condition we used is that the new dictionary should only contain the details of the students whose Marks are greater than 350.

So we have set a variable called mr that stores the threshold of marks. Next, an empty list called nlist is created to store the result.

A for loop with an iterator variable called d is initialized to run through every item in the dictionary dictn to check if the condition is satisfied. The condition is given by the if statement. The key value which satisfies this condition is appended to the new list with the help of append function.

Next, the new list is printed.

Lastly, we are converting the list to a dictionary and printing the filtered dictionary.

Filter List Of Dictionaries Using For Loop
Filter List Of Dictionaries Using For Loop

The complete output is shown below.

The filtered list is
[{'Name': 'Ajay', 'Age': 12, 'Grade': 7, 'Marks': 400}, {'Name': 'Bindhu', 'Age': 14, 'Grade': 8, 'Marks': 450}, {'Name': 'Dikshit', 'Age': 13, 'Grade': 8, 'Marks': 560}, {'Name': 'Akarsh', 'Age': 14, 'Grade': 9, 'Marks': 456}, {'Name': 'Fathima', 'Age': 15, 'Grade': 10, 'Marks': 458}]
The filtered dictionary using for loop is:
{'Ajay': {'Name': 'Ajay', 'Age': 12, 'Grade': 7, 'Marks': 400}, 'Bindhu': {'Name': 'Bindhu', 'Age': 14, 'Grade': 8, 'Marks': 450}, 'Dikshit': {'Name': 'Dikshit', 'Age': 13, 'Grade': 8, 'Marks': 560}, 'Akarsh': {'Name': 'Akarsh', 'Age': 14, 'Grade': 9, 'Marks': 456}, 'Fathima': {'Name': 'Fathima', 'Age': 15, 'Grade': 10, 'Marks': 458}}

Conclusion

To conclude we have learned what is a dictionary and how a dictionary stores the data in the form of key-value pairs where the keys should be unique and the values may not be unique.

We have seen a few operations on the dictionary like creating a dictionary, printing the keys and values of the dictionary, deleting a certain key from the dictionary, and removing the last key-value pair from the dictionary.

Coming to filtering the dictionary based on the key values, we have seen three approaches.

Firstly, we created a list of dictionaries according to the syntax and used list comprehension to set a condition and print the filtered list of dictionaries that satisfy the given condition. This list is then converted to a dictionary.

In the second approach, we have used the built-in function –filter and the lambda function to do the same task.

Lastly, we used a for loop to iterate through every dictionary to check if the item satisfies the condition which is then appended to the empty list we created. This list is then converted to a dictionary.

References

You can find more about the filter function in the official documentation.

Refer to this stack overflow answer chain on the same topic.