07-22-2022, 10:33 PM
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?
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.
In this example, the 30-Year-Fixed mortgage rate is referenced by name]) and its value is incremented by one (1).
Then, this updated value is saved back to and output to the terminal.
Method 2: Use Dictionary.get()
This method uses the method and passes a Dictionary Key as an argument to retrieve and increment the corresponding Value.
In this example, the 20 Year Fixed mortgage rate is referenced and accessed using). This value is then incremented by two (2).
Then, this updated value is saved back to and output to the terminal.
Method 3: Use Dictionary.update()
This method uses to reference the appropriate Key and increment the Value.
In this example, the current value of a 10 Year Variable mortgage rate is retrieved, incremented by three (3) and saved to.
Next, the appropriate Key for the key:value pair is accessed, and the Value is updated to reflect the contents of and output to the terminal.
Method 4: Use Unpacking
This method uses to increment the Value of a key:value pair.
Above, accesses the 7 Year Variable mortgage rate, increments the rate to the amount indicated (), and outputs it to the terminal.
Method 5: Use Dictionary Comprehension
This method increments all Mortgage Rates in one fell swoop using.
Above, accesses each mortgage rate in the Dictionary and increments the rate by 1.10. Then, is called to round the results down to three (3) decimal places. The result saves to and output to the terminal.
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
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
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}
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
- Method 2: Use Code:
Dictionary.get()
- Method 3: Use Code:
Dictionary.update()
- Method 4: Use Code:
Unpacking using the * Operator
- Method 5: Use Code:
Dictionary Comprehension
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'Then, this updated value is saved back to
Code:
mtg_rates['30-Year-Fixed']Code:
6.625Method 2: Use Dictionary.get()
This method uses the
Code:
Dictionary.get()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')Then, this updated value is saved back to
Code:
mtg_rates['20-Year-Fixed']Code:
7.25Method 3: Use Dictionary.update()
This method uses
Code:
Dictionary.update()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_rateNext, the appropriate Key for the key:value pair is accessed, and the Value is updated to reflect the contents of
Code:
new_rateCode:
8.125Method 4: Use Unpacking
This method uses
Code:
UnpackingCode:
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.011Code:
9.011Method 5: Use Dictionary Comprehension
This method increments all Mortgage Rates in one fell swoop using
Code:
Dictionary ComprehensionCode:
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_ratesCode:
round()Code:
new_ratesCode:
{'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
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]

