'Using dictionary for conditional assignment

How can I use dictionary as a conditional statement to change the value of the variable?

for example:

a = 20

dicta = {10:3, 20:2, 30:1}

#compare using first pair as the value of a, such that:

if a=10: assign a=3
if a=20: assign a=2
if a=30: assign a=1

Thankyou!



Solution 1:[1]

Try this:

a = 20

for k, v in dicta.items():
  if k == a:
    a = v
    break
      

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ahtisham