0

Suppose I have the following dictionary:

{'delta': -1.7127264925486676, 'beta': 0.30069982893451086, 'gamma': 1.3575879024686477}

How can I really assign those values to the variables delta, beta, and gamma?

Currently, if I do print(beta), then it only returns me beta instead of the value in the dictionary. What should I do to assign the values to the variables?

Thanks!

ZR-
  • 809
  • 1
  • 4
  • 12

2 Answers2

1

Try it like the below :

mydic = {'delta': -1.7127264925486676, 'beta': 0.30069982893451086, 'gamma': 1.3575879024686477}
# print(beta) # Occurs an error
print('beta')
print(mydic['beta'])
print(mydic.get('beta'))
Kimpro
  • 81
  • 2
  • 11
0

You can call .values() on the dictionary and unpack them directly into the variables

a,b,g=(my_dict.values())

b
0.30069982893451086

However, it's important to know that in versions prior to 3.7, dictionary entries are not ordered, so you can't rely on this type of unpacking unless you're on a newer version

G. Anderson
  • 5,815
  • 2
  • 14
  • 21