Create an account


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?


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





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





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





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)



https://www.sickgaming.net/blog/2022/07/...ary-value/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,379 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] How I Built an English Dictionary App with Django xSicKxBot 0 1,550 03-29-2023, 04:46 AM
Last Post: xSicKxBot
  [Tut] Can a Python Dictionary Have a List as a Value? xSicKxBot 0 1,164 10-15-2022, 08:22 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Values Without “dict_values” xSicKxBot 0 1,362 10-12-2022, 02:48 AM
Last Post: xSicKxBot
  [Tut] Python Print Dictionary Without One Key or Multiple Keys xSicKxBot 0 1,255 10-10-2022, 08:56 PM
Last Post: xSicKxBot
  [Tut] How to Get a Random Entry from a Python Dictionary xSicKxBot 0 1,259 09-15-2022, 02:08 PM
Last Post: xSicKxBot
  [Tut] How to Find the Most Common Element in a Python Dictionary xSicKxBot 0 1,096 09-09-2022, 12:16 PM
Last Post: xSicKxBot
  [Tut] Convert CSV to Dictionary in Python xSicKxBot 0 1,251 06-19-2022, 04:13 AM
Last Post: xSicKxBot
  [Tut] How to Save a Dictionary to a File in Python xSicKxBot 0 1,258 06-15-2022, 03:15 AM
Last Post: xSicKxBot
  [Tut] How to Create a Dictionary from two Lists xSicKxBot 0 1,207 04-27-2022, 05:37 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016