lssee
lssee

Reputation: 23

How to return to previous value in dictionary given a specific value?

I have different tax rates for different income levels.

I am trying to return the previous tax rate from the tax rate acquired from a function. How can I get it to return to the previous tax rate?

tax_levels = [
{
    "min": 0,
    "max": 999,
    "rate": 1,
},
{
    "min": 1000,
    "max": 1999,
    "rate": 5
},
]

current_rate = 5
previous_rate = ??

I am trying to get it to return to 1 for previous_rate using the variable current_rate.

Upvotes: 1

Views: 54

Answers (2)

seanong
seanong

Reputation: 3

tax_level[0]['rate']

tax_level is a list containing dictionaries. tax_level[0] will give you the first dictionary in the list. ['rate'] will give you the value of the key 'rate' in the first dictionary in the tax_level list of dictionaries.

Upvotes: 0

Pranav Void
Pranav Void

Reputation: 357

You can define a function

def previous_tax_rate(current_rate, tax_levels):
    for index, tax_level in enumerate(tax_levels):
        if tax_level['rate'] == current_rate:
            return tax_levels[index - 1]['rate']

Upvotes: 1

Related Questions