![]() |
|
[Tut] How to Increment a Dictionary Value - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Increment a Dictionary Value (/thread-99731.html) |
[Tut] How to Increment a Dictionary Value - xSicKxBot - 07-22-2022 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} 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'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]
|