Python - Product and Inter Summation dictionary values


Introduction

This article will help us to understand the dictionary and how we can get its values with the help of its key. In Python, the dictionary is a collection of key, value pairs. We are going to learn about calculating the product and summation of dictionary values using the values() method.

To calculate the sum of dictionary values we are going to use a loop to traverse through each value present in the dictionary and add it to the result variable(initially which is 0) and keep on updating it with each value until all the values of the dictionary have been processed.

Similarly, to calculate the product of the dictionary value. We will initialize the resulting variable with a value of 1, and keep multiplying with the dictionary's values until all the dictionary values have been processed.

Syntax

The method used in the article is −

dictionary.values()

The value() method takes no arguments. This method returns a view object. The view object contains the values of the dictionary as a list.

Approach

Let us now understand the approaches to solving our problem.

Approach to calculate the sum of the dictionary values

Let us now look into the approach of the following program. This is broken down into the following steps.

  • First we have defined a function whose name is "calculate_sum" which will take a dictionary as an argument in a variable.

  • After that, a variable whose name is "Sum" is defined, which is going to hold the total sum of the dictionary values.

  • Then we will use a loop to iterate through the dictionary's values.

  • In the for loop each value of the dictionary is added to the sum variable.

  • The "Sum" variable is then returned from the "calculate_sum" function.

  • A dictionary is defined outside the function which has three values i.e. {'a': 11, 'b': 12, 'c': 13}

  • After this a variable named "total" is declared that is going to hold the return value of the "calculate_sum" function.

  • And then finally in the end we will print the result on the console screen.

In this approach, we pass a dictionary to the function traversing through the dictionary's values to calculate the total sum of the values and store that value in the variable and then return the total sum.

Code for the above approach

Example

Now let us understand our problem with a proper example,

# Function to calculate
def calculate_sum(dictionary):
   # Declaring variable to store total value
   Sum = 0
   # Looping through dictionary values
   for value in dictionary.values():
# Storing sum into "Sum" variable
      Sum += value
   return Sum
# Defining dictionary
dicti = {'a': 11, 'b': 12, 'c': 13}
total = calculate_sum(dicti)
# Printing sum
print("Sum: ", total)

Output

Sum:  36

let us take a dictionary with the values {'a': 11, 'b': 12, 'c': 13}. As the flow of our program, this dictionary will be passed to the function calculate sum. Now this function will take the first value of the dictionary i.e. 11 and add it to the variable sum then it will take the second value of the dictionary 12 and add it to the sum variable after that it will take the last value 13 and add it the sum variable after adding all the values of the dictionary it will return the total value i.e. 36 from the function which will be stored in the total variable. This total value will then be displayed on the console screen.

Approach to calculating the product of the dictionary values

Now let us understand the approach to our problem which is explained in the following steps.

  • First we have defined a function whose name is "dictionary_product" which will take a dictionary as an argument.

  • Then inside that function a variable named "product" is defined which will store the product of the values of the dictionary initially the value of the variable is set to 1.

  • After that a loop is used to go through the values of the dictionary.

  • In the for loop each value of the dictionary is multiplied by the product variable and gets stored in the product variable.

  • The "product" variable is then returned from the "dictionary_product" function.

  • Outside the function a dictionary is defined which has three values i.e. {'a': 2, 'b': 3, 'c': 4, 'd': 5}

  • After that a variable named "product" is declared outside the function which will hold the returned value of the "dictionary_product" function.

  • Finally, we will print the result on the console screen.

Code for the above approach

Example

Now let us understand our problem with a proper example,

# Defining a function to calculate product
def dictionary_product(dictionary):
   # Declaring variable to store product
   product = 1
   # Looping through dictionary values
   for value in dictionary.values():
      product *= value
   return product
# Declaring a dictionary
dicti = {'a': 2, 'b': 3, 'c': 4, 'd': 5}
# Calling dictionary_product function
product = dictionary_product(dicti)
# Printing result
print("Product:", product)

Output

Product: 120

let us take a dictionary with the values {'a': 2, 'b': 3, 'c': 4, 'd': 5}. As the flow of our program, this dictionary will be passed to the function dictionary product which will traverse through the values of the dictionary. First, it will multiply "2" with the product variable then it will multiply "3" then "4" and at last "5" with the product variable and then it will return the product i.e. 120 to print on the console screen.

Conclusion

In this article, We have learned how we can calculate the sum of the dictionary values and how to calculate the product of the dictionary value with the help of the in-built values() function. We have also got to know both ways with an example. Knowing how to calculate the sum and product of the dictionary values is very much useful in real life. Some of the real-world applications are useful in financial portfolio analysis, it is also useful in data analysis, scientific research, etc.

This will also help us improve our programming skills and develop our understanding of Python.

Updated on: 04-Oct-2023

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements