

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Python program to find the sum of all items in a dictionary
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a dictionary, and we need to print the 3 highest value in a dictionary.
Three approaches to the problem statement are given below:
Approach 1 − Calculating sum from the dictionary iterable
Example
# sum function def Sum(myDict): sum_ = 0 for i in myDict: sum_ = sum_ + myDict[i] return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))
Output
Sum of dictionary values : 14
Approach 2 − Calculating the sum from the dictionary.values() iterable
Example
# sum function def Sum(dict): sum_ = 0 for i in dict.values(): sum_ = sum_ + i return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))
Output
Sum of dictionary values : 14
Approach 3 − Calculating the sum from the dictionary.values() iterable
Example
# sum function def Sum(dict): sum_ = 0 for i in dict.keys(): sum_ = sum_ + dict[i] return sum_ # Driver Function dict = {'T': 1, 'U':2, 'T':3, 'O':4, 'R':5} print("Sum of dictionary values :", Sum(dict))
Output
Sum of dictionary values : 14
Conclusion
In this article, we have learned how we can find the highest 3 values in a dictionary
- Related Questions & Answers
- Python Program to Multiply All the Items in a Dictionary
- Program to find sum of the sum of all contiguous sublists in Python
- Python Program to Find the Sum of all Nodes in a Tree
- Program to find sum of all elements of a tree in Python
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Program to find sum of beauty of all substrings in Python
- Program to find the sum of all digits of given number in Python
- Program to find sum of the 2 power sum of all subarray sums of a given array in Python
- Program to find sum of all odd length subarrays in Python
- How to sum values of a Python dictionary?
- Python Program to find the sum of array
- Program to find XOR sum of all pairs bitwise AND in Python
- Python program to find sum of absolute difference between all pairs in a list
- Python program to find the highest 3 values in a dictionary
- How to print all the keys of a dictionary in Python?
Advertisements