Product of two Dictionary Keys in Python


Introduction

Product of two dictionaries in python invovles going through dictionaries. We can find out specific keys that meet certain conditions. Then the product can be calculated easily. Dictionary in python is quite similar to that in real world. In the English dictionary the words are written in the form of key value pair. In a similar way data is stored in python dictionaries. In the below paragraph, we will break down the process of finding the product of two dictionary keys in python.

Breaking Down the Process

Understanding the Dictionary

Dictionary can be described as the set of keys and value pair. Keys may include diffterent types of data such as numbers, strings. For example in the below program, “my_dict” is initialized with four key and value pairs. “apple” key is initilized with value 5. “Banana” key is initilized with value 10, “orange” key is initilized with value 3“, grape” key is initilized with value 8.

Solving the Problem with Dictionary Comprehension and keys():

Example

my_dict = {
    'apple': 5,
    'banana': 10,
    'orange': 3,
    'grape': 8
 }

filtered_keys = [key for key in my_dict.keys() if my_dict[key] >= 5]
product = 1
for key in filtered_keys:
    product *= my_dict[key]
print("Product of the selected keys:", product)

Output

Product of the selected keys: 400

Above code is helpful in finding the product of corresponding dictionary key values. We have initialized the variable “my_dict”. Inside this variable we have declared four dictionary key value pairs. Each key is assigned with a numerical value. This statement “filtered_keys = [key for key in my_dict.keys() if my_dict[key] >= 5]” will filter out keys whose value is greater than or equal to 5. We have initialized the product variable to one. For loop will be used for the iteration in filtered keys. Product of the values of the dictionary keys is calculated with the help of this “product *= my_dict[key]” formula. Then in the end we have successfully printed the product on the screen.

Solving the "Product of Two Dictionary Keys" problem using the Counter() function from the collections module and the * operator:

Example

from collections import Counter

my_dict = {
    'apple': 5,
    'banana': 10,
    'orange': 3,
    'grape': 8
}

filtered_keys = [key for key in my_dict if my_dict[key] >= 5]

counter = Counter(filtered_keys)

product = 1
for key in filtered_keys:
    product *= my_dict[key]

print("Product of the selected keys:", product)

Output

Product of the selected keys: 400

From the module collections we are importing counter. Modules are the set of already written instructions in python that can used by the developers or programmers. There is no need of writing the code again we can directly import someone else code in python with the help of these modules. For example in the above program, “my_dict” is initialized with four key and value pairs. “apple” key is initilized with value 5. “Banana” key is initilized with value 10, “orange” key is initilized with value 3“, grape” key is initilized with value 8. Similar to above solution we have used the for loop and we have compared the keys value with the help of the syntx “[key for key in my_dict if my_dict[key] >= 5]” . counter function is used to separate out the filtered keys. Product here is intialized to 1.

Solving the "Product of Two Dictionary Keys" problem using dictionaries and the zip() function:

Example

my_dict = {
    'apple': 5,
    'banana': 10,
    'orange': 3,
    'grape': 8
}

filtered_keys = [key for key in my_dict if my_dict[key] >= 5]

filtered_values = [my_dict[key] for key in filtered_keys]

product = 1
for value in filtered_values:
    product *= value

print("Product of the selected keys:", product)

Output

Product of the selected keys: 400

Similar to the first code, We have initialized the variable “my_dict”. Inside this variable we have declared four dictionary key value pairs. Each key is assigned with a numerical value. “apple” key is initilized with value 5. “Banana” key is initilized with value 10, “orange” key is initilized with value 3“, grape” key is initilized with value 8. This statement “filtered_keys = [key for key in my_dict.keys() if my_dict[key] >= 5]” will filter out keys whose value is greater than or equal to 5. In the next line of code, we will filter out the values of the keys which are greater than or equal to 5. We have initialised the value of product equal to 1. Filtered_values are iterated using for loop. Then we will calculate the product of these keys. Finally we will print the product of selected tuple keys in python.

Updated on: 27-Jul-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements