- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 Multiply All the Items in a Dictionary
When it is required to multiply all the elements in a dictionary, the key values in the dictionary are iterated over. The key is multiplied with the previous key, and output is determined.
The dictionary is a set of key-value pairs.
Example
Below is a demonstration for the same −
my_dict = {'Jane':99,'Will':54,'Mark':-3} my_result = 2 for key in my_dict: my_result = my_result * my_dict[key] print("The reuslt of multiplying keys in a dictionary is : ") print(my_result)
Output
The reuslt of multiplying keys in a dictionary is : -32076
Explanation
- A dictionary is defined.
- A variable is assigned a certain value.
- The ‘key’ in the dictionary is iterated over.
- In every step, this key is multiplied with the variable previously assigned.
- This value is assigned to the same variable itself.
- This is displayed on the console as the output.
Advertisements