GeeksforGeeks App
Open App
Browser
Continue

# Python – Binary operation on specific keys in Dictionary List

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, ‘geeks’: 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``, ``'geeks'``: ``10``},``             ``{``'Gfg'``: ``9``, ``'is'``: ``4``, ``'best'``: ``10``, ``'for'``: ``4``, ``'geeks'``: ``7``},``             ``{``'Gfg'``: ``2``, ``'is'``: ``10``, ``'best'``: ``8``, ``'for'``: ``9``, ``'geeks'``: ``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))`

Output :

The original list is : [{‘geeks’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘geeks’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘geeks’: 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``, ``'geeks'``: ``10``},``             ``{``'Gfg'``: ``9``, ``'is'``: ``4``, ``'best'``: ``10``, ``'for'``: ``4``, ``'geeks'``: ``7``},``             ``{``'Gfg'``: ``2``, ``'is'``: ``10``, ``'best'``: ``8``, ``'for'``: ``9``, ``'geeks'``: ``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))`

Output :

The original list is : [{‘geeks’: 10, ‘for’: 8, ‘Gfg’: 5, ‘is’: 6, ‘best’: 7}, {‘geeks’: 7, ‘for’: 4, ‘Gfg’: 9, ‘is’: 4, ‘best’: 10}, {‘geeks’: 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

1. Use map() function to apply a lambda expression to each dictionary in the list
2. In the lambda expression, access the values of the ‘Gfg’ and ‘best’ keys and multiply them
3. The resulting map object will contain the results of multiplication for each dictionary
4. Convert the map object to a list and enumerate() to create a list of tuples with index and result
5. Print the result

## Python3

 `# initializing list``test_list ``=` `[{``'Gfg'``: ``5``, ``'is'``: ``6``, ``'best'``: ``7``, ``'for'``: ``8``, ``'geeks'``: ``10``},``             ``{``'Gfg'``: ``9``, ``'is'``: ``4``, ``'best'``: ``10``, ``'for'``: ``4``, ``'geeks'``: ``7``},``             ``{``'Gfg'``: ``2``, ``'is'``: ``10``, ``'best'``: ``8``, ``'for'``: ``9``, ``'geeks'``: ``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))`

Output

`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 ‘geeks’, 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``, ``'geeks'``: ``10``},``             ``{``'Gfg'``: ``9``, ``'is'``: ``4``, ``'best'``: ``10``, ``'for'``: ``4``, ``'geeks'``: ``7``},``             ``{``'Gfg'``: ``2``, ``'is'``: ``10``, ``'best'``: ``8``, ``'for'``: ``9``, ``'geeks'``: ``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))`

Output

```The original list is : [{'Gfg': 5, 'is': 6, 'best': 7, 'for': 8, 'geeks': 10}, {'Gfg': 9, 'is': 4, 'best': 10, 'for': 4, 'geeks': 7}, {'Gfg': 2, 'is': 10, 'best': 8, 'for': 9, 'geeks': 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

My Personal Notes arrow_drop_up