Problem Formulation and Solution Overview
Method 1: Use sum() and dict.values()
You can use the functions sum()
and dict.values()
to sum all values of a Python dictionary with the expression sum(dict.values())
.
Here’s how this applies to our example scenario:
wages = {'01': 910.56, '02': 1298.68, '03': 1433.99, '04': 1050.14, '05': 877.67} total = sum(wages.values()) print('Total Wages: ${0:,.2f}'.format(total))
This code declares a Dictionary, wages
containing five (5) key:value pairs of monthly income.
Option 1 above tallies the extracted values and saves to total
. Then using format()
, the output is modified to display a Dollar sign ($
), a comma (if needed), and restrict the output to two (2) decimal places.
Total Wages: $5,571.04 |
OR
total = round(sum(wages.values()), 2) print(total)
Option 2 above tallies the extracted values, rounds the result to two (2) decimal places, and saves to total
. The result is output to the terminal as is.
5571.04 |
Method 2: Use sum() and Generator Expression
You can combine the built-in sum()
function with a simple generator expression to sum all values in a Python dictionary with the expression sum(my_dict[item] for item in my_dict)
.
Here’s the example:
wages = {'01': 910.56, '02': 1298.68, '03': 1433.99, '04': 1050.14, '05': 877.67} total = sum(wages[item] for item in wages) print('Total Wages: ${0:,.2f}'.format(total))
This code declares a Dictionary, wages
containing five (5) key:value pairs of monthly income.
A one-line for
loop is used to navigate each Dictionary item (value) and tally the items. The result saves to total
.
Then, format()
is used to modify the output to display a Dollar sign ($
), a comma (if needed), and restrict the output to two (2) decimal places.
Total Wages: $5,571.04 |
Method 3: Use a Lambda
An alternative way to sum over Python dictionary values is to use the functools.reduce()
method in combination with a lambda function in the expression functools.reduce(lambda tot, v: tot+my_dict[v], my_dict, 0)
.
Here’s what that looks like in our example scenario:
wages = {'01': 910.56, '02': 1298.68, '03': 1433.99, '04': 1050.14, '05': 877.67} total = functools.reduce(lambda tot, v: tot+wages[v], wages, 0) print(total)
First, this code declares a Dictionary, wages
containing five (5) key:value pairs of monthly income.
Then, the functools.reduce()
function is used with a Lambda to tally the wages. This result saves to total
and is output to the terminal as is.
Output
5571.04 |
Method 4: Use a Generator
To sum all values in a Python dictionary, use sum()
and a list representation of the dictionary values obtained with my_dict.values()
and using list()
for the conversion.
wages = {'01': 910.56, '02': 1298.68, '03': 1433.99, '04': 1050.14, '05': 877.67} print(sum(list(wages.values())))
First, this code declares a Dictionary, wages
containing five (5) key:value pairs of monthly income.
Then, the values are converted into a List format. Finally, the sum()
function is applied to this List and output to the terminal.
Output
5571.04 |
Method 5: Simple For Loop
You can sum all values in a Python dictionary by iterating over all dictionary values using a for
loop and adding them one by one.
Here’s our running example:
wages = {'01': 910.56, '02': 1298.68, '03': 1433.99, '04': 1050.14, '05': 877.67} total = 0 for value in wages.values(): total += value print(total) # 5571.04
This is not the most Pythonic approach, however. A generator expression would be more concise and more readable.
Summary
These five (5) methods of summing Dictionary values should give you enough information to select the best one for your coding requirements.
Good Luck & Happy Coding!