
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Extract Dictionaries with Given Key from a List in Python
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.
In this article, we will learn a python program to extract dictionaries with a given Key from a list of dictionaries.
Methods Used
The following are the various methods to accomplish this task:
Using list comprehension and keys() function
Using filter() and lambda functions
Using operator.countOf() method
Example
Assume we have taken an input list containing dictionaries and an input key. We will now extract the dictionaries from the list containing the given input key using the above methods.
Input
inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 12, 'users': 15, 'hello': 18}, {'users': 21}] inputKey = 'users'
Output
The resultant list of dictionaries containing the 'users' key ? [{'users': 15, 'hello': 18}, {'users': 21}]
In the above input list of dictionaries only the {'users': 12, 'users': 15, 'hello': 18}, {'users': 21} dictionaries contain the input key users. Hence these dictionaries are extracted from the input list.
Using list comprehension and keys() function
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
keys() function(the dict. keys() method provides a view object that displays a list of all the keys in the dictionary in order of insertion)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input list of dictionaries.
Print the input list.
Create another variable to store the input key.
Traverse in the given list of dictionaries using the list comprehension and check whether the key is present in the keys of the dictionary using the in operator.
If it is true then store this dictionary in the list.
Print the resultant list of dictionaries containing the input key.
Example
The following program returns a list of dictionaries containing the input key from an input dictionary using list comprehension and keys() function -
# input list of dictionaries inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 12, 'users': 15, 'hello': 18}, {'users': 21}] # printing input list print("Input List:\n", inputList) # input key inputKey = 'users' # Traversing in the given input list and checking # whether the input Key exists in the list of dictionaries keys resultantDict = [e for e in inputList if inputKey in list(e.keys())] # printing resultant list of dictionaries containing input key print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)
Output
Input List: [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}] The Resultant list of dictionaries containing the 'users' key: [{'users': 15, 'hello': 18}, {'users': 21}]
Using filter() and lambda functions
filter() function ? filters the specified sequence using a function that determines if each element in the sequence is to be true or false.
lambda function
A lambda function is an anonymous function that is small.
A lambda function can have an unlimited/any number of arguments but only one expression.
Syntax
lambda arguments : expression
Example
The following program returns a list of dictionaries containing the input key from an input dictionary using filter() and lambda functions -
# input list of dictionaries inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 12, 'users': 15, 'hello': 18}, {'users': 21}] # printing input list print("Input List:\n", inputList) # input key inputKey = 'users' # Filtering all the dictionaries with the given input key resultantDict = list( filter(lambda sub: inputKey in list(sub.keys()), inputList)) # printing the resultant list of dictionaries containing input key print("Resultant list of dictionaries containing 'users' key:\n", resultantDict)
Output
On executing, the above program will generate the following output -
Input List: [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}] Resultant list of dictionaries containing 'users' key: [{'users': 15, 'hello': 18}, {'users': 21}]
Using operator.countOf() method
The countOf() function of the operator module returns the number of elements in a that are equal to b.
Syntax
operator.countOf(a, b)
Parameters
a ? list or string or any other data type.
b ? value for which we must count the number of occurrences in "a"
Example
The following program returns a list of dictionaries containing the input key from an input dictionary using list comprehension and keys() function -
import operator as op # input list of dictionaries inputList = [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 12, 'users': 15, 'hello': 18}, {'users': 21}] # printing input list print("Input List:\n", inputList) # input key inputKey = 'users' # Traversing in the given input list and checking # whether the input Key exists in the list of dictionaries keys resultantDict = [e for e in inputList if op.countOf( list(e.keys()), inputKey) > 0] # printing resultant list of dictionaries containing input key print("The Resultant list of dictionaries containing the 'users' key:\n", resultantDict)
Output
On executing, the above program will generate the following output -
Input List: [{'hello': 3, 'tutorialspoint': 6}, {'python': 9}, {'users': 15, 'hello': 18}, {'users': 21}] The Resultant list of dictionaries containing the 'users' key: [{'users': 15, 'hello': 18}, {'users': 21}]
Conclusion
In this article, we learned how to use three distinct techniques to retrieve dictionaries with a particular key from a list of dictionaries. The new method has been learned. Use operator.countOf() to count the iterable's elements. To determine whether an element exists or not, the new approach can be used in place of the "in" and "not in" operators.