Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform certain operations over some keys in the dictionary throughout the list and return the result. This kind of problem is common in domains involving data like web development. Let’s discuss certain ways in which this task can be performed.
Input : test_list = [{‘best’: 8, ‘Gfg’: 13}, {‘is’: 5, ‘for’: 9, ‘best’: 15, ‘Gfg’: 7, ‘neveropen’: 7}]
Output : [(0, 104), (1, 105)]Input : test_list = [{‘Gfg’ : 3, ‘best’ : 10}]
Output : [(0, 30)]
Method #1 : loop + enumerate()
The combination of above functions can be used to solve this problem. This is a brute-force approach in which we loop through each dictionary list and perform operations and store results.
Python3
# Python3 code to demonstrate working of # Binary operation on specific keys in Dictionary List # Using loop + enumerate() # initializing list test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'neveropen' : 10 }, { 'Gfg' : 9 , 'is' : 4 , 'best' : 10 , 'for' : 4 , 'neveropen' : 7 }, { 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'neveropen' : 3 }] # printing original list print ( "The original list is : " + str (test_list)) # initializing keys op1_key = 'Gfg' op2_key = 'best' # Binary operation on specific keys in Dictionary List # Using loop + enumerate() res = [] for idx, val in enumerate (test_list): res.append((idx, val[op1_key] * val[op2_key])) # printing result print ( "The constructed result list : " + str (res)) |
The original list is : [{‘neveropen’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘neveropen’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘neveropen’: 3, ‘for’: 9, ‘Gfg’: 2, ‘is’: 10, ‘best’: 8}] The constructed result list : [(0, 35), (1, 90), (2, 16)]
Time complexity: O(N), where N is the number of dictionaries in the list test_list. This is because the program iterates over each dictionary in the list once.
Auxiliary space: O(N), where N is the number of dictionaries in the list test_list. This is because the program creates a list res with N elements, where each element is a tuple of two values.
Method #2: Using list comprehension + enumerate()
The combination of above functions can also be used to solve this problem. In this, we perform similar task using list comprehension in shorthand manner.
Python3
# Python3 code to demonstrate working of # Binary operation on specific keys in Dictionary List # Using list comprehension + enumerate() # initializing list test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'neveropen' : 10 }, { 'Gfg' : 9 , 'is' : 4 , 'best' : 10 , 'for' : 4 , 'neveropen' : 7 }, { 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'neveropen' : 3 }] # printing original list print ( "The original list is : " + str (test_list)) # initializing keys op1_key = 'Gfg' op2_key = 'best' # Binary operation on specific keys in Dictionary List # Using list comprehension + enumerate() res = [(idx, val[op1_key] * val[op2_key]) for idx, val in enumerate (test_list)] # printing result print ( "The constructed result list : " + str (res)) |
The original list is : [{‘neveropen’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘neveropen’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘neveropen’: 3, ‘for’: 9, ‘Gfg’: 2, ‘is’: 10, ‘best’: 8}] The constructed result list : [(0, 35), (1, 90), (2, 16)]
Time Complexity: O(n*n) where n is the number of elements in the dictionary. The list comprehension + enumerate() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the dictionary.
Method #3: Using map() function and lambda expression
- Use map() function to apply a lambda expression to each dictionary in the list
- In the lambda expression, access the values of the ‘Gfg’ and ‘best’ keys and multiply them
- The resulting map object will contain the results of multiplication for each dictionary
- Convert the map object to a list and enumerate() to create a list of tuples with index and result
- Print the result
Python3
# initializing list test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'neveropen' : 10 }, { 'Gfg' : 9 , 'is' : 4 , 'best' : 10 , 'for' : 4 , 'neveropen' : 7 }, { 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'neveropen' : 3 }] # initializing keys op1_key = 'Gfg' op2_key = 'best' # Binary operation on specific keys in Dictionary List # Using map() function and lambda expression res = list ( enumerate ( map ( lambda x: x[op1_key] * x[op2_key], test_list))) # printing result print ( "The constructed result list : " + str (res)) |
The constructed result list : [(0, 35), (1, 90), (2, 16)]
Time complexity: O(n), where n is the length of the list of dictionaries.
Auxiliary space: O(n), where n is the length of the list of dictionaries.
Method #4: Using for loop and dictionary access
Create a list of dictionaries called test_list containing three dictionaries with keys ‘Gfg’, ‘is’, ‘best’, ‘for’, and ‘neveropen’, each with a corresponding integer value.
Print the original test_list.
Initialize two keys, op1_key and op2_key, with the values ‘Gfg’ and ‘best’, respectively.
Create an empty list called res to store the results.
Loop through test_list using enumerate() to get the index and dictionary value of each element.
Access the values corresponding to op1_key and op2_key in the current dictionary using dictionary access val[op1_key] and val[op2_key], and multiply them.
Append the result of the multiplication and the current index as a tuple to the res list using the append() method.
Print the resulting list res.
Python3
# initializing list test_list = [{ 'Gfg' : 5 , 'is' : 6 , 'best' : 7 , 'for' : 8 , 'neveropen' : 10 }, { 'Gfg' : 9 , 'is' : 4 , 'best' : 10 , 'for' : 4 , 'neveropen' : 7 }, { 'Gfg' : 2 , 'is' : 10 , 'best' : 8 , 'for' : 9 , 'neveropen' : 3 }] # printing original list print ( "The original list is : " + str (test_list)) # initializing keys op1_key = 'Gfg' op2_key = 'best' # Binary operation on specific keys in Dictionary List # Using for loop and dictionary access res = [] for idx, val in enumerate (test_list): res.append((idx, val[op1_key] * val[op2_key])) # printing result print ( "The constructed result list : " + str (res)) |
The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'neveropen': 10}, {'Gfg': 9, 'is': 4, 'best': 10, 'for': 4, 'neveropen': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'neveropen': 3}] The constructed result list : [(0, 35), (1, 90), (2, 16)]
The time complexity of this method is O(n), where n is the length of the list.
The auxiliary space is also O(n), as we are creating a list to store the results