Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Increment a Dictionary Value
1
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?

Code:
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}

[Image: 1f4ac.png] 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.

Code:
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
Code:
(mtg_rates['30-Year-Fixed'
]) and its value is incremented by one (1).

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

Code:
6.625



Method 2: Use Dictionary.get()

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

Code:
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
Code:
(mtg_rates.get('20-Year-Fixed')
). This value is then incremented by two (2).

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

Code:
7.25



Method 3: Use Dictionary.update()

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

Code:
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
Code:
new_rate
.

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

Code:
8.125



Method 4: Use Unpacking

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

Code:
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 (
Code:
9.011
), and outputs it to the terminal.

Code:
9.011

Method 5: Use Dictionary Comprehension

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

Code:
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
Code:
mtg_rates
Dictionary and increments the rate by 1.10. Then,
Code:
round()
is called to round the results down to three (3) decimal places. The result saves to
Code:
new_rates
and output to the terminal.

Code:
{'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

[Image: image-133.png][i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: