Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to extract dictionaries with maximum number of keys
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 from a list of dictionaries.
Problem Overview
Given a list containing multiple dictionaries, we need to find and extract the dictionary(ies) that have the maximum number of keys.
Example
Consider the following input list ?
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
print("Input:", inputList)
Input: [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15}, {'good': 3, 'website': 9}]
The second dictionary {'hello': 5, 'tutorialspoint': 10, 'users': 15} has the maximum number of keys (3), so it should be extracted.
Method 1: Using for loop and len() function
This approach iterates through each dictionary and tracks the one with the maximum length using len() function ?
# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
{'good': 3, 'website': 9}]
print("Input list of dictionaries:")
print(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)
print("Resultant dictionary having maximum no of keys:")
print(resultantDict)
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}
Method 2: Using list comprehension and max() function
This approach uses list comprehension to find all dictionaries with the maximum length. It returns a list containing all dictionaries that have the maximum number of keys ?
# input list containing dictionaries
inputList = [{'tutorialspoint': 10}, {'hello': 5, 'tutorialspoint': 10, 'users': 15},
{'good': 3, 'website': 9}]
print("Input list of dictionaries:")
print(inputList)
# getting the maximum length using max() function and list comprehension
maxLength = max(len(p) for p in inputList)
# finding all dictionaries with the maximum length
resultantDict = [p for p in inputList if len(p) == maxLength]
print("Resultant dictionary having maximum no of keys:")
print(resultantDict)
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}]
Handling Multiple Dictionaries with Same Maximum Length
When multiple dictionaries have the same maximum length, the second method returns all of them ?
# input list with multiple dictionaries having same max length
inputList = [{'a': 1, 'b': 2}, {'x': 10, 'y': 20}, {'p': 100}]
maxLength = max(len(d) for d in inputList)
result = [d for d in inputList if len(d) == maxLength]
print("Dictionaries with maximum keys:", result)
print("Maximum length:", maxLength)
Dictionaries with maximum keys: [{'a': 1, 'b': 2}, {'x': 10, 'y': 20}]
Maximum length: 2
Comparison
| Method | Returns | Best For |
|---|---|---|
| For loop with len() | Single dictionary | When you need only one result |
| List comprehension with max() | List of dictionaries | When multiple dictionaries may have max length |
Conclusion
Both methods effectively extract dictionaries with maximum keys. Use the for loop approach when you need a single dictionary, and list comprehension when you want all dictionaries with maximum length.
