How to Increment a Dictionary Value

5/5 - (1 vote)

Problem Formulation and Solution Overview

In this article, you’ll learn how to increment a dictionary value in Python.

To make it more fun, we have the following running scenario:

Trinity Bank keeps track of its latest mortgage rates in a Dictionary format. Recently there has been a steady increase in rates. How would Trinity Bank increment/increase the Dictionary value of a selected rate?

mtg_rates = {'30-Year-Fixed': 5.625,
'20-Year-Fixed': 5.250,
'10-Year-Variable': 5.125,
'7-Year-Variable': 5.011,
'5-Year-Variable': 4.625}

πŸ’¬ Question: How would we write code to increment a Dictionary value in Python?

We can accomplish this task by one of the following options:


Method 1: Use Dictionary Key Name

This method passes an existing Dictionary Key Name to access and increment the appropriate Value.

mtg_rates['30-Year-Fixed'] += 1
print(mtg_rates['30-Year-Fixed'])

In this example, the 30-Year-Fixed mortgage rate is referenced by name (mtg_rates['30-Year-Fixed']) and its value is incremented by one (1).

Then, this updated value is saved back to mtg_rates['30-Year-Fixed'] and output to the terminal.

6.625
Python Dictionary – The Ultimate Guide

Method 2: Use Dictionary.get()

This method uses the Dictionary.get() method and passes a Dictionary Key as an argument to retrieve and increment the corresponding Value.

mtg_rates['20-Year-Fixed'] = mtg_rates.get('20-Year-Fixed') + 2
print(mtg_rates['20-Year-Fixed'])

In this example, the 20 Year Fixed mortgage rate is referenced and accessed using (mtg_rates.get('20-Year-Fixed')). This value is then incremented by two (2).

Then, this updated value is saved back to mtg_rates['20-Year-Fixed'] and output to the terminal.

7.25
Python dict.get() Method

Method 3: Use Dictionary.update()

This method uses Dictionary.update() to reference the appropriate Key and increment the Value.

new_rate = mtg_rates['10-Year-Variable'] + 3
mtg_rates.update({'10-Year-Variable': new_rate})
print(mtg_rates['10-Year-Variable'])

In this example, the current value of a 10 Year Variable mortgage rate is retrieved, incremented by three (3) and saved to new_rate.

Next, the appropriate Key for the key:value pair is accessed, and the Value is updated to reflect the contents of new_rate and output to the terminal.

8.125
Python Dictionary update() Method

Method 4: Use Unpacking

This method uses Unpacking to increment the Value of a key:value pair.

mtg_rates = {**mtg_rates, '7-Year-Variable': 9.011}
print(mtg_rates)

Above, accesses the 7 Year Variable mortgage rate, increments the rate to the amount indicated (9.011), and outputs it to the terminal.

9.011

Method 5: Use Dictionary Comprehension

This method increments all Mortgage Rates in one fell swoop using Dictionary Comprehension.

new_rates = dict(((key, round(value*1.10, 3)) for key, value in mtg_rates.items()))
print(new_rates)

Above, accesses each mortgage rate in the mtg_rates Dictionary and increments the rate by 1.10. Then, round() is called to round the results down to three (3) decimal places. The result saves to new_rates and output to the terminal.

{'30-Year-Fixed': 6.188, '20-Year-Fixed': 5.775, '10-Year-Variable': 5.638, '7-Year-Variable': 5.512, '5-Year-Variable': 5.088}

Summary

These methods of incrementing a Dictionary Value should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!


Regex Humor

Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)