Python - Scoring Matrix using Dictionary Last Updated : 14 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Sometimes, while working with Python records, we can have a problem in which we need to resolve scoring in Python Matrix records. This means mapping of each key of dictionary with its value to aggregate score of each row. This kind of problem can have applications in gaming and web development domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [['gfg', 'best'], ['geeks'], ['is', 'for']] Output : [18, 15, 12] Input : test_list = [['gfg', 'geeks', 'CS']] Output : [20] Method #1 : Using loop This is one of the way in which this task can be performed. In this, we iterate for the matrix elements and perform the values substitutions using dictionary and perform row summations. Python3 # Python3 code to demonstrate working of # Scoring Matrix using Dictionary # Using loop # initializing list test_list = [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']] # printing original list print("The original list is : " + str(test_list)) # initializing test dict test_dict = {'gfg' : 5, 'is' : 10, 'best' : 13, 'for' : 2, 'geeks' : 15} # Scoring Matrix using Dictionary # Using loop res = [] for sub in test_list: sum = 0 for val in sub: if val in test_dict: sum += test_dict[val] res.append(sum) # printing result print("The Row scores : " + str(res)) Output : The original list is : [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']] The Row scores : [28, 32] Time complexity: O(M^N) as the number of combinations generated is M choose N. Auxiliary space: O(L) as the size of the resultant list is L. Method #2 : Using list comprehension + sum() This is yet another way to solve this problem. In this, we perform the summation using sum() and list comprehension is used for iterations and score assignments. Python3 # Python3 code to demonstrate working of # Scoring Matrix using Dictionary # Using list comprehension + sum() # initializing list test_list = [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']] # printing original list print("The original list is : " + str(test_list)) # initializing test dict test_dict = {'gfg' : 5, 'is' : 10, 'best' : 13, 'for' : 2, 'geeks' : 15} # Scoring Matrix using Dictionary # Using list comprehension + sum() res = [sum(test_dict[word] if word.lower() in test_dict else 0 for word in sub) for sub in test_list] # printing result print("The Row scores : " + str(res)) Output : The original list is : [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']] The Row scores : [28, 32] Time complexity: O(M^N) as the number of combinations generated is M choose N.Auxiliary space: O(L) as the size of the resultant list is L. Comment More infoAdvertise with us Next Article Python - Scoring Matrix using Dictionary manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads Python - Sorting a dictionary of tuples This task becomes particularly useful when working with structured data, where tuples represent grouped information (e.g., names and scores, items and prices). Sorting such data enhances its readability and usability for further analysis.For example, consider a dictionary d = {'student3': ('bhanu', 3 min read Python - Convert Matrix to Dictionary The task of converting a matrix to a dictionary in Python involves transforming a 2D list or matrix into a dictionary, where each key represents a row number and the corresponding value is the row itself. For example, given a matrix li = [[5, 6, 7], [8, 3, 2], [8, 2, 1]], the goal is to convert it i 4 min read Mapping Matrix with Dictionary-Python The task of mapping a matrix with a dictionary involves transforming the elements of a 2D list or matrix using a dictionary's key-value pairs. Each element in the matrix corresponds to a key in the dictionary and the goal is to replace each matrix element with its corresponding dictionary value. For 4 min read Python - Dictionary Values Mean Given a dictionary, find the mean of all the values present. Input : test_dict = {"Gfg" : 4, "is" : 4, "Best" : 4, "for" : 4, "Geeks" : 4} Output : 4.0 Explanation : (4 + 4 + 4 + 4 + 4) / 4 = 4.0, hence mean. Input : test_dict = {"Gfg" : 5, "is" : 10, "Best" : 15} Output : 10.0 Explanation : Mean of 4 min read Sort a Dictionary - Python In Python, dictionaries store key-value pairs and are great for organizing data. While they werenât ordered before Python 3.7, you can still sort them easily by keys or values, in ascending or descending order. Whether youâre arranging names alphabetically or sorting scores from highest to lowest, P 5 min read Python - Nested Dictionary values summation Sometimes, while working with Python dictionaries, we can have problem in which we have nested records and we need cumulative summation of it's keys values. This can have possible application in domains such as web development and competitive programming. Lets discuss certain ways in which this task 8 min read Python Sort Nested Dictionary by Multiple Values We are given a nested dictionary and our task is to sort a nested dictionary by multiple values in Python and print the result. In this article, we will see how to sort a nested dictionary by multiple values in Python. Example: Input : {'A': {'score': 85, 'age': 25}, 'B': {'score': 92, 'age': 30}, ' 3 min read Python - Sort Dictionary by Values Summation Give a dictionary with value lists, sort the keys by summation of values in value list. Input : test_dict = {'Gfg' : [6, 7, 4], 'best' : [7, 6, 5]} Output : {'Gfg': 17, 'best': 18} Explanation : Sorted by sum, and replaced. Input : test_dict = {'Gfg' : [8], 'best' : [5]} Output : {'best': 5, 'Gfg': 4 min read Iterate Python Dictionary Using Enumerate() Function Python dictionaries are versatile data structures used to store key-value pairs. When it comes to iterating through the elements of a dictionary, developers often turn to the enumerate() function for its simplicity and efficiency. In this article, we will explore how to iterate through Python dictio 3 min read Python | Sort dictionary by value list length While working with Python, one might come to a problem in which one needs to perform a sort on dictionary list value length. This can be typically in case of scoring or any type of count algorithm. Let's discuss a method by which this task can be performed. Method 1: Using sorted() + join() + lambda 4 min read Like