
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 Maximum Number of Keys 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 the maximum number of keys.
Methods Used
The following are the various methods to accomplish this task:
Using for loop and len() function
Using list comprehension and max() function
Example
Assume we have taken an input list containing multiple dictionaries. We will now extract dictionaries having a maximum number of keys using the above methods.
Input
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
Output
Resultant dictionary having maximum no of keys: [{'hello': 5, 'tutorialspoint': 10, 'users': 15}]
In the above example, the second dictionary {'hello': 5, 'tutorialspoint': 10, 'users': 15} has a maximum number of keys i.e, 3. So, it is extracted from the input dictionary.
Using for loop and len() function
The number of items in an object is returned by the len() method.The len() function returns the number of characters in a string when the object is a string.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input list containing dictionaries.
Print the input list.
Create a new empty dictionary to store the resultant dictionary.
Initialize the maximum length as 0 for storing the maximum length.
Use the for loop to traverse through each element of the input list.
Use the if conditional statement to check whether the length of the current element is greater than the maximum length using len() function(returns the number of items in an object).
Assign that current element to the resultant dictionary if the condition is true.
Assign the length of a current element as the maximum length using len() function.
Print the resultant dictionary having a maximum number of keys in the input list.
The following program returns a dictionary with the maximum number of keys in an input list of dictionaries using for loop and len() function -
# input list containing dictionaries inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}] # printing input list print("Input list of dictionaries:\n", inputList) # creating a new empty dictionary resultantDict = dict() # initializing with 0 for storing maximum length maxLength = 0 # traversing through each element of the input list for p in inputList: # checking whether the length of the current element is # greater than the maximum length if len(p) > maxLength: # assigning that current element to the resultant dictionary resultantDict = p # assigning the length of a current element as the maximum length maxLength = len(p) # printing resultant dictionary having a maximum no of keys in the input list print("Resultant dictionary having maximum no of keys:\n", resultantDict)
Output
On executing, the above program will generate the following output -
Input list of dictionaries: [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}] Resultant dictionary having maximum no of keys: {'hello': 5, 'tutorialspoint': 10, 'users': 15}
Using list comprehension and max() function
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
max() function(returns the highest-valued item/greatest number in an iterable)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Create a variable to store the input list containing dictionaries.
Print the input list.
Traverse in the input list of dictionaries using list comprehension and get the length of the dictionary.
Find the maximum length of the dictionaries in the input list using the max() function.
Traverse in the input list of dictionaries using list comprehension and get the dictionary with this length.
Print the resultant dictionary having a maximum number of keys in the input list.
Example
The following program returns a dictionary with the maximum number of keys in an input list using list comprehension and max() function -
# input list containing dictionaries inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}] # printing input list of dictionaries print("Input list of dictionaries:\n", inputList) # getting the lengths of each dictionary element using list comprehension # getting the maximum length from this list using the max() function maxLength = max(len(p) for p in inputList) # Finding the dictionary with the length as the maximum length resultantDict = [p for p in inputList if len(p) == maxLength] # printing resultant dictionary having maximum no. of keys in the input list print("Resultant dictionary having maximum no of keys:\n", resultantDict)
Output
On executing, the above program will generate the following output -
Input list of dictionaries: [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}] Resultant dictionary having maximum no of keys: [{'hello': 5, 'tutorialspoint': 10, 'users': 15}]
Conclusion
In this article, we have learned how to extract dictionaries with a maximum number of keys using 2 different methods. In order to solve problems quickly, we also learned how to use list comprehension and simple, concise syntax.