Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to find the sum of dictionary keys
In this article, we will learn different Python methods to find the sum of dictionary keys. Python provides several built-in functions that make this task efficient and straightforward.
Methods Overview
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 Input and Expected Output
Assume we have an input dictionary containing numeric keys. We will find the sum of all the keys using different 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 is 4+1+10+2+8 = 25.
Method 1: Using For Loop
The most straightforward approach is to iterate through all keys using a for loop and accumulate their sum ?
# 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)
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
The dict.keys() function returns a view object containing all dictionary keys. We can pass this directly to sum() function for a more concise solution ?
# input dictionary
inputDict = {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
print("Input Dictionary:", inputDict)
# getting the sum of dictionary keys directly
keysSum = sum(inputDict.keys())
print("Sum of keys of an input dictionary:", keysSum)
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
The reduce() function from the functools module applies a function cumulatively to items in an iterable. We can use it with a lambda function to sum all keys ?
# 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)
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
The items() function returns key-value pairs as tuples. We can iterate through these tuples and sum only the keys ?
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)
Input Dictionary: {4: 10, 1: 30, 10: 50, 2: 70, 8: 90}
Sum of keys of an input dictionary: 25
Comparison of Methods
| Method | Code Length | Readability | Best For |
|---|---|---|---|
| For Loop | Medium | High | Beginners, custom logic |
| sum() + keys() | Short | High | Simple, efficient solution |
| reduce() | Short | Medium | Functional programming style |
| items() | Medium | High | When you need both keys and values |
Conclusion
The most efficient and readable approach is using sum(inputDict.keys()). Use the for loop method when you need additional processing logic, and consider reduce() for functional programming patterns.
