- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Multiplication across Like Keys Value list elements
In the given problem statement we have to calculate the multiplication of the same key value in the given dictionary. So we will be solving this problem by implementing the code in Python.
Understanding the Problem
The problem at hand is to perform the multiplication on the like keys in a given list of dictionaries using Python. So we will be having the list of dictionaries as the input and this data will contain key-value pairs in which the keys will be identical in the dictionaries. So we have to multiply the respective values of the same keys.
Logic for The Above Problem
To solve the above problem, we will implement the simple logic in Python. We will iterate the list of dictionaries and keep saving the product of each key we encountered. For every key we will extract the key and values and update the multiplication of the corresponding values. If we found the key for the first time then we will initialize the product for the current value. And if this key is encountered again then we will multiply the product with the previous value.
Algorithm
Step 1 − First initialize the blank dictionary as multiply_dict. This dictionary will be used to store the products for every key.
Step 2 − After that we will iterate over each dictionary in the given input list.
Step 3 − Then we will iterate over the key and value pairs with the help of items methos for each dictionary.
Step 4 − In this step we will do certain processes for each key value pairs. First we will check if the key is not present in the multiply_dict, if this condition is true then we will add it and initialize the value with the current value.
Step 5 − Next, we will check another condition if the key is already present in the multiply_dict then multiply the current value with the existing value in the multiply_dict.
Step 6 − After traversing over the dictionaries multiply_dict will be having the product for each key in the given dictionaries.
Example
# Define function to multiply the same key values def multiply_values(content): # Initialize empty dictionary multiply_dict = {} # Iterate over key-value pairs for key_value in content: for key, value in key_value.items(): if key not in multiply_dict: multiply_dict[key] = value else: multiply_dict[key] *= value return multiply_dict # Initialize the data content = [ {'Grape': 3, 'Peach': 5, 'Cherry': 2}, {'Grape': 4, 'Peach': 4, 'Cherry': 5}, {'Grape': 5, 'Peach': 2, 'Cherry': 3} ] # Call the function Output = multiply_values(content) # Print the result print(Output)
Output
{'Grape': 3, 'Peach': 5, 'Cherry': 2}
Complexity
Let’s suppose that n is the number of dictionaries in the given list and m is the average number of key-value pairs in each dictionary, then the time complexity for calculating the multiplication across same keys value in the list components is O(n * m). The reason for this complexity is that we have iterated over every dictionary and performed constant time operations to get the product for values.
Conclusion
As we have successfully implemented the code for getting the multiplication of the same key values in the given dictionaries in the list. This is an efficient approach to get the desired result.
To Continue Learning Please Login
Login with Google