
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Sum of Dictionary Keys in Python
In this article, we will learn a python program to find the sum of dictionary keys.
Methods Used
The following are the various methods to accomplish this task ?
Using for loop
Using dict.keys() and sum() functions
Using reduce() function
Using items() function
Example
Assume we have taken an input dictionary containing some random key-value pairs. We will now find the sum of all the keys of an input dictionary using the above methods.
Input
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Output
Sum of keys of an input dictionary: 25
In the above example, the sum of keys of an input dictionary is 4+1+10+2+8 = 25.
Method 1: Using For Loop
Algorithm (Steps)
Following are the Algorithms/steps to be followed to perform the desired task -.
Create a variable to store the input dictionary.
Print the input dictionary.
Initialize a keysSum variable with 0 for the resultant storing sum of keys of an input dictionary.
Use the for loop to traverse through the keys of an input dictionary.
Use the + operator to add the current key to the keysSum variable.
Print the resultant sum of keys of an input dictionary.
Example
The following program returns the sum of all the keys of an input dictionary using the for loop -
# input dictionary inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} # printing the input dictionary print("Input Dictionary: ", inputDict) # storing the sum of keys of the input dictionary keysSum = 0 # traversing through the keys of an input dictionary for k in inputDict: # adding the current key to keysSum variable keysSum += k # printing the sum of keys of an input dictionary print("Sum of keys of an input dictionary:", keysSum)
Output
On execution, the above program will generate the following output -
Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} Sum of keys of an input dictionary: 25
Method 2: Using dict.keys() and sum() functions
dict.keys() function ? provides a view object that displays a list of all the keys in the dictionary in order of insertion
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Use the dict.keys() function to get the keys of the input dictionary and convert them into a list using list() function(returns a list of an iterable).
Get the sum of the dictionary keys using the sum() function(returns the sum of all items in an iterable).
Print the resultant sum of all the keys of an input dictionary.
Example
The following program returns the sum of all the keys of an input dictionary using dict.keys() and sum() functions -
# input dictionary inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} print("Input Dictionary: ", inputDict) # getting the keys of the input dictionary and converting them into the list # and then getting the sum of dictionary keys keysSum = sum(list(inputDict.keys())) print("Sum of keys of an input dictionary:", keysSum)
Output
On execution, the above program will generate the following output -
Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} Sum of keys of an input dictionary: 25
Method 3: Using reduce( ) function
reduce() function
In Python, the reduce() function iterates through each item in a list or other iterable data type, returning a single value. It's in the functools library. This is more efficient than looping.
Syntax
reduce(function, iterable)
Lambda Function
Lambda Function, often known as an 'Anonymous Function,' is the same as a normal Python function except that it can be defined without a name. The def keyword is used to define normal functions, while the lambda keyword is used to define anonymous functions. They are, however, limited to a single line of expression. They, like regular functions, can accept several parameters.
Syntax
lambda arguments: expression
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -
Use the import keyword to import reduce function from functools module.
Add the consecutive dictionary keys using the lambda function and reduce the size to make a single element(sum of all keys).
Print the resultant sum of all the keys of an input dictionary.
Example
The following program returns the sum of all the keys of an input dictionary using reduce( ) function -
# importing reduce function from functools module from functools import reduce inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} print("Input Dictionary: ", inputDict) # Add the keys and reduce it to a single number(sum) using reduce() function keysSum = reduce(lambda p, q: p + q, inputDict) print("Sum of keys of an input dictionary:", keysSum)
Output
On execution, the above program will generate the following output -
Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} Sum of keys of an input dictionary: 25
Method 4: Using items() function
Use the dictionary.items() function to traverse through the tuples in a dictionary with the for loop.
The items() function returns a view object i.e, it contains the key-value pairs of the dictionary, as tuples in a list.
Example
The following program returns the sum of all the keys of an input dictionary using the items() function and for loop -
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} print("Input Dictionary: ", inputDict) # storing the sum of keys of the input dictionary keysSum = 0 # traversing through the keys, and values of an input dictionary for key, value in inputDict.items(): # adding the current key to keysSum variable keysSum += key print("Sum of keys of an input dictionary:", keysSum)
Output
On execution, the above program will generate the following output -
Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90} Sum of keys of an input dictionary: 25
Conclusion
In this article, we learned 4 different methods for computing the dictionary's total key's sum. We also learned how to add conditions to the iterable items using the lambda function and reduce.