Python - Column Maximum in Dictionary Value Matrix
Last Updated :
10 May, 2023
Given a Dictionary with Matrix Values, compute maximum of each column of those Matrix.
Input : test_dict = {"Gfg" : [[7, 6], [3, 2]],
"is" : [[3, 6], [6, 10]],
"best" : [[5, 8], [2, 3]]}
Output : {'Gfg': [7, 6], 'is': [6, 10], 'best': [5, 8]}
Explanation : 7 > 3, 6 > 2, hence ordering.
Input : test_dict = {"Gfg" : [[7, 6], [3, 2]],
"is" : [[3, 6], [6, 10]]}
Output : {'Gfg': [7, 6], 'is': [6, 10]}
Explanation : 6 > 3, 10 > 6, hence ordering.
Method #1 : Using dictionary comprehension + sorted() + items()
This is one of the ways in which this task can be performed. In this, The inner columns are extracted and sorted and last value of sorted list(maximum) is returned as result. This happens for all list values using dictionary comprehension.
Python3
# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using dictionary comprehension + sorted() + items()
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
"is" : [[4, 6], [6, 8]],
"best" : [[7, 4], [2, 3]]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# sorted() used to sort and "-1" used to get last i.e
# maximum element
res = {key : sorted(val, key = lambda ele : (ele[0], ele[1]))[-1] for key, val in test_dict.items()}
# printing result
print("The evaluated dictionary : " + str(res))
OutputThe original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}
Time Complexity: O(n*nlogn), where n is the values in dictionary
Auxiliary Space: O(n), where n is the size of dictionary
Method #2 : Using max() + map() + zip()
This is one of the ways in which this task can be performed. In this, we extract maximum using max(), and align columns to list using zip() and map() is used to extend logic of zip to each column.
Python3
# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using max() + map() + zip()
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
"is" : [[4, 6], [6, 8]],
"best" : [[7, 4], [2, 3]]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# map extending logic to entire columns
# result compiled using dictionary comprehension
res = {key: list(map(max, zip(*val))) for key, val in test_dict.items()}
# printing result
print("The evaluated dictionary : " + str(res))
OutputThe original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}
Method #3: Using list comprehension + map() + lambda function
Step-by-step approach:
- We initialize a dictionary called test_dict with three keys "Gfg", "is", and "best". Each key has a value that is a list of lists representing a matrix with two rows and two columns.
- We print the original dictionary using the print() function and a string concatenation that includes the string "The original dictionary is : " and the string representation of the test_dict dictionary using the str() function.
- We use a list comprehension with a map() function and a lambda function to find the maximum value of each column in each matrix of the test_dict dictionary. The map() function applies the lambda function to each tuple of values obtained by transposing the matrix using the zip() function. The list() function creates a list from the resulting map() object. Finally, the list comprehension creates a new dictionary with the same keys as test_dict but with the maximum values of each column as values.
- We print the resulting dictionary using the print() function and a string concatenation that includes the string "The evaluated dictionary : " and the string representation of the res dictionary using the str() function.
Python3
# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using list comprehension + map() + lambda function
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
"is" : [[4, 6], [6, 8]],
"best" : [[7, 4], [2, 3]]}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# using list comprehension + map() + lambda function
res = {key: list(map(lambda x: max(x), zip(*val))) for key, val in test_dict.items()}
# printing result
print("The evaluated dictionary : " + str(res))
OutputThe original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}
Time complexity: O(n * m), where n is the number of keys in the dictionary, and m is the length of the value list for each key.
Auxiliary space: O(m), where m is the length of the value list for each key.
Similar Reads
Python - Maximum Value in Nested Dictionary
Sometimes, while working with python dictionary, we can have problem in which each key in itself is a record with several keys and we desire to substitute the value as maximum value of keys of dictionary. This kind of problem can have application in many domains that involves data. Lets discuss cert
4 min read
Python - Maximum record value key in dictionary
Sometimes, while working with dictionary records, we can have a problem in which we need to find the key with maximum value of a particular key of nested records in list. This can have applications in domains such as web development and Machine Learning. Lets discuss certain ways in which this task
6 min read
Python - Maximum value assignment in Nested Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign to the outer key, the item with maximum value in inner keys. This kind of problem can occur in day-day programming and web development domains. Let's discuss a way in which this task can be performed.
3 min read
Python - Maximum available value Dictionaries
Given list of dictionaries and a list, extract all the dictionaries which contain maximum available value of key from list. Input : test_list [{"Gfg" : 6, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}, {"Gfg" : 2, "is" : 16, "best" : 10}], K = "best", arg_list = [10, 7, 6, 12] Output :
4 min read
Python - Extract Maximum Keys' value dictionaries
Given a dictionary, extract all the dictionary which contains a any key which has maximum values among other keys in dictionary list. Input : [{"Gfg" : 3, "is" : 7, "Best" : 8}, {"Gfg" : 9, "is" : 2, "Best" : 9}, {"Gfg" : 5, "is" : 4, "Best" : 10}, {"Gfg" : 3, "is" : 6, "Best" : 14}] Output : [{"Gfg
6 min read
Python - Column-wise elements in Dictionary value list
Given dictionary with value list, extract elements columnwise. Input : test_dict = {'Gfg' : [3, 6], 'is' : [4, 2], 'best' :[9, 1]} Output : [3, 4, 9, 6, 2, 1] Explanation : 3, 4, 9 from col1 and then 6, 2, 1 from 2 are extracted in order. Input : test_dict = {'Gfg' : [3], 'is' : [4], 'best' :[9]} Ou
3 min read
Python - Key Columns Dictionary from Matrix
Given a Matrix and list of keys, map each column's values with a custom list key. Input : test_list1 = [[4, 6], [8, 6]], test_list2 = ["Gfg", "Best"]Â Output : {'Gfg': [4, 8], 'Best': [6, 6]}Â Explanation : Column wise, Key values assignment.Input : test_list1 = [[4], [6]], test_list2 = ["Gfg"]Â Out
6 min read
Python - Columns to Dictionary Conversion in Matrix
Given a Matrix, Convert to Dictionary with elements in 1st row being keys, and subsequent rows acting as values list. Input : test_list = [[4, 5, 7], [10, 8, 4], [19, 4, 6], [9, 3, 6]] Output : {4: [10, 19, 9], 5: [8, 4, 3], 7: [4, 6, 6]} Explanation : All columns mapped with 1st row elements. Eg. 4
3 min read
Python | N largest values in dictionary
Many times while working with a Python dictionary, we can have a particular problem finding the N maxima of values in numerous keys. This problem is quite common while working in the web development domain. Let's discuss several ways in which this task can be performed. Method #1 : itemgetter() + it
5 min read
Dictionary items in value range in Python
In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop.Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items
2 min read