Python Program that displays the key of list value with maximum range
Last Updated :
16 May, 2023
Given a Dictionary with keys and values that are lists, the following program displays key of the value whose range in maximum.
Range = Maximum number-Minimum number
Input : test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]}
Output : Best
Explanation : 9 - 0 = 9, Maximum range compared to all other list given as values
Input : test_dict = {"Gfg" : [16, 2, 4, 1], "Best" : [1, 0, 9, 3]}
Output : Gfg
Explanation : 16 - 1 = 15, Maximum range compared to all other list given as values
Method 1: Using max(), min() and loop
In this, we get max() and min() of each list and perform difference to find range. This value is then stored and max difference of all such values is computed by applying max() at the result list.
Python3
# initializing dictionary
test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
max_res = 0
for sub, vals in test_dict.items():
# Storing maximum of difference
max_res = max(max_res, max(vals) - min(vals))
if max_res == max(vals) - min(vals):
res = sub
# printing result
print("The maximum element key : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Time Complexity: O(n) where n is the number of elements in the dictionary “test_dict”. The sorted and itemgetter function is used to perform the task and it takes O(n*logn) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary “test_dict”.
Method 2: Using list comprehension, max() and min()
In this, we compute the maximum range, and then extract the key which matches that difference using list comprehension.
Python3
# initializing dictionary
test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# getting max value
max_res = max([max(vals) - min(vals) for sub, vals in test_dict.items()])
# getting key matching with maximum value
res = [sub for sub in test_dict if max(test_dict[sub]) - min(test_dict[sub]) == max_res][0]
# printing result
print("The maximum element key : " + str(res))
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Method 3: Finding the Key with the Maximum Difference in Values in a Dictionary.
- Initialize two variables max_diff and max_key to None.
- Iterate over the items in the dictionary using a for loop.
- For each item, calculate the difference between its maximum and minimum value using max() and min() functions.
- If max_diff is None or the difference for the current item is greater than max_diff, update max_diff and max_key to the current difference and key respectively.
- After the loop, max_key will contain the key with the maximum difference.
Python3
# initializing dictionary
test_dict = {"Gfg" : [6, 2, 4, 1], "is" : [4, 7, 3, 3, 8], "Best" : [1, 0, 9, 3]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# alternative approach
max_diff = None
max_key = None
for key, value in test_dict.items():
diff = max(value) - min(value)
if max_diff is None or diff > max_diff:
max_diff = diff
max_key = key
# printing result
print("The maximum element key : " + str(max_key))
OutputThe original dictionary is : {'Gfg': [6, 2, 4, 1], 'is': [4, 7, 3, 3, 8], 'Best': [1, 0, 9, 3]}
The maximum element key : Best
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(1), as it only uses a constant amount of space to store the max_diff and max_key variables.
Method 4: Using the built-in 'map' function
- Define a function that computes the difference between the maximum and minimum value of a list.
- Use the 'map' function to apply the function to each value of the dictionary.
- Find the index of the maximum difference using the 'index' method of the resulting list.
- Use the 'list' method to retrieve the keys of the dictionary and get the corresponding key.
Python3
# initializing dictionary
test_dict = {"Gfg": [6, 2, 4, 1], "is": [4, 7, 3, 3, 8], "Best": [1, 0, 9, 3]}
# function to compute the difference between the maximum and minimum value of a list
def max_min_diff(lst):
return max(lst) - min(lst)
# computing difference between max and min values of each list in the dictionary using map function
diffs = list(map(max_min_diff, test_dict.values()))
# finding the index of maximum difference
max_idx = diffs.index(max(diffs))
# retrieving corresponding key from dictionary
max_key = list(test_dict.keys())[max_idx]
# printing result
print("The maximum element key : " + str(max_key))
OutputThe maximum element key : Best
Time complexity: O(n*m), where n is the number of keys and m is the length of the longest list in the dictionary.
Auxiliary space: O(n), for creating the list of differences.
Similar Reads
Python program that renders a dictionary from two list with K values
Given two list, one will be used to provide keys to dictionary and the other for values. The following program takes K values from second list and assigns it to each key, creating a dictionary of the following kind: Input : test_list = ["gfg", "best"], val_list = [1, 4, 5, 6, 7, 8, 8, 5], K = 4 Outp
5 min read
Python Program to display keys with same values in a dictionary List
Given a list with all dictionary elements, the task is to write a Python program to extract keys having similar values across all dictionaries. Examples: Input : test_list = [{"Gfg": 5, "is" : 8, "best" : 0}, {"Gfg": 5, "is" : 1, "best" : 0}, {"Gfg": 5, "is" : 0, "best" : 0}] Output : ['Gfg', 'best'
5 min read
Python program to find the key of maximum value tuples in a dictionary
Given a dictionary with values as tuples, the task is to write a python program to find the key of maximum value tuples. Examples: Input : test_dict = {'gfg' : ("a", 3), 'is' : ("c", 9), 'best' : ("k", 10), 'for' : ("p", 11), 'geeks' : ('m', 2)}Output : forExplanation : 11 is maximum value of tuple
6 min read
Python program to find Maximum value from dictionary whose key is present in the list
Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list. Examples: Input : test_dict = {"Gfg": 4, "is" : 5, "best" : 10, "for" : 11, "geeks" : 3}, test_list = ["Gfg", "best", "geeks"] Output : 10 Explanation : Max value is 11, but not
6 min read
Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python program to find second maximum value in Dictionary
Dictionaries in Python is same as Associative arrays or Maps in other languages. Unlike lists, dictionaries stores data in key-value pair.Let's see how to find the second maximum value in a Python dictionary.Method #1: Naive Approach: A general approach is to find the largest value of the dictionary
3 min read
Python | Tuples with maximum key of similar values
Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find maximum key-value pair. This kind of problem can occur while working with data. Let's discuss certain ways in which this task can be done. Method
6 min read
Python Program to extract dictionaries with maximum number of keys
Given a list of dictionaries, the task is to write a Python program to extract a dictionary with a maximum number of keys. Input : test_list = [{'Gfg' : 1}, {'Gfg' : 1, 'is' : 5, 'best' : 4}, {'Gfg' : 2, 'best' : 9}] Output : {'Gfg' : 1, 'is' : 5, 'best' : 4} Explanation : Dictionary with max. lengt
7 min read
Python | Find the sublist with maximum value in given nested list
Given a list of list, the task is to find sublist with the maximum value in second column. Examples: Input : [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]] Output :['Labs', 532] Input: [['Geek', 90], ['For', 32], ['Geeks', 120]] Output: ['Geeks', 120] Below are some tasks
4 min read
Maximum String Value Length of Key - Python
The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
3 min read