Open In App

Python – Maximum of filtered Keys in dictionary

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes while working with Python dictionaries, we might have a problem in which we require to just maximum the selective key values from the dictionary. This problem can occur in web development domain. Let’s discuss certain ways in which this problem can be solved. 

Method #1: Using list comprehension + get() + max() The combination of the above functions can be used to perform this particular task. In this, we access the values using the get method and traverse the dictionary using list comprehension. We perform maximum using max(). 

Python3




# Python3 code to demonstrate working of
# Maximum of filtered Keys
# Using list comprehension + get() + max()
 
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize key list
key_list = ['gfg', 'best', 'CS']
 
# Using list comprehension + get() + max()
# Maximum of filtered Keys
res = max([test_dict.get(key) for key in key_list])
     
# printing result
print("The maximum of Selective keys : " + str(res))


Output : 

The original dictionary : {'for': 4, 'gfg': 1, 'is': 2, 'best': 3, 'CS': 5}
The maximum of Selective keys : 5

Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

  Method #2 : Using itemgetter() + max() This single function can be used to perform this particular task. It is built in to perform this specific task. It takes chain of keys and returns the corresponding values as a tuple which can be type casted. We perform maximum using max(). 

Python3




# Python3 code to demonstrate working of
# Maximum of filtered Keys
# Using itemgetter() + max()
from operator import itemgetter
 
# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize key list
key_list = ['gfg', 'best', 'CS']
 
# Using itemgetter() + max()
# Maximum of filtered Keys
res = max(list(itemgetter(*key_list)(test_dict)))
     
# printing result
print("The maximum of Selective keys : " + str(res))


Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The maximum of Selective keys : 5

Method #3: Using a lambda function with the map() function:

Step-by-Step Algorithm :

  1. Initialize a dictionary test_dict with key-value pairs.
  2. Initialize a list key_list with a selective set of keys from the dictionary.
  3. Use the map() function with a lambda function to create a new list of values from test_dict corresponding to the keys in key_list.
  4. For each key in key_list, call the get() method on test_dict with the key as the argument to retrieve the value associated with the key.
  5. If the key is not present in the dictionary, return float(‘-inf’) as the default value.
  6. Use the max() function to find the maximum value in the new list of values.
    Print the maximum value found in step 4 as the output.

Python




# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize key list
key_list = ['gfg', 'best', 'CS']
 
# Find maximum value among selective keys
max_value = max(map(lambda key: test_dict.get(key, float('-inf')), key_list))
 
# Print result
print("The maximum of Selective keys : " + str(max_value))


Output

The original dictionary : {'CS': 5, 'is': 2, 'gfg': 1, 'best': 3, 'for': 4}
The maximum of Selective keys : 5

Complexity Analysis : 

Time Complexity: O(n)  where n is the number of keys in the key_list.
This is because the map() function creates a new list of values by applying the lambda function to each key in the key_list  and the max() function has a time complexity of O(n). So the overall time complexity of this code is O(n) + O(n) = O(2n) = O(n)

Auxiliary Space: O(n)  where n is the number of keys in the key_list.
This is because the map() function creates a new list of values that has the same length as the key_list. 

Method #4: Using loop and setdefault()

  • Initializing the dictionary test_dict 
  • Initializing the list of keys to filter key_list.
  • setdefault() function returns the value of the key if it exists in the dictionary, else it sets the default value to the key and returns it.
  • comparing the value of max_value with the returned value and setting max_value to the greater of the two.

Python3




# Initialize dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Initialize key list
key_list = ['gfg', 'best', 'CS']
 
# Find maximum value among selective keys
max_value = 0
for key in key_list:
    max_value = test_dict.setdefault(key, max_value)
 
# Print result
print("The maximum of Selective keys : " + str(max_value))


Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The maximum of Selective keys : 5

Time Complexity: O(N) where N is the number of items in the dictionary.

Space Complexity: O(1), as we are not using any extra memory.

Method#5: Using a for loop and try-except block

  • Initialize a variable maximum with the minimum possible value using float(‘-inf’).
  • Traverse through each key of the key_list using a for loop.
  • Inside the loop, try to access the value of the current key in test_dict using try and except block.
  • If the value of the current key is greater than maximum, update the maximum value with the current value of the key.
  • Print the maximum value of the selected keys using print().

Python3




# initializing dictionary
test_dict = {'gfg' : 1, 'is' : 2, 'best' : 3, 'for' : 4, 'CS' : 5}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# initializing key list
key_list = ['gfg', 'best', 'CS']
 
# initializing maximum variable with minimum possible value
maximum = float('-inf')
 
# finding maximum value among selected keys
for key in key_list:
   try:
       if test_dict[key] > maximum:
           maximum = test_dict[key]
   except:
       pass
 
# printing result
print("The maximum of Selective keys : " + str(maximum))


Output

The original dictionary : {'gfg': 1, 'is': 2, 'best': 3, 'for': 4, 'CS': 5}
The maximum of Selective keys : 5

Time Complexity: O(N) where N is the number of items in the dictionary.

Auxiliary Space: O(1), as we are not using any extra memory.



Last Updated : 27 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads