user3144095
user3144095

Reputation: 501

How to set value in python dictionary while looking it up from another dictionary?

I have two dictionaries. The first is mapping_dictionary, it has several key-value pairs. It will serve as a reference. The second dictionary only has two key-value pairs. I would like to look up the value that should be assigned to the second dictionary in the mapping_dictionary and set it to one of the values. I tried doing it a few different ways but no success.

Please let me know if the syntax is wrong or if this is not the way to do something like this in Python? Thank you in advance for any help.

Example 1:

mapping_dictionary={'TK_VAR_DEC':1, 'TK_ID':2, 'TK_COMMA':3}
token_dictionary={'TK_TYPE', 'TK_VALUE'}

tk_v=mapping_dictionary.get("TK_VAR_DEC") 

token_dictionary['TK_TYPE']=tk_v
token_dictionary['TK_VALUE']="VAR_DEC"

Example 2:

token_dictionary['TK_TYPE']=mapping_dictionary.get("TK_VAR_DEC")
token_dictionary['TK_VALUE']="VAR_DEC"

Upvotes: 0

Views: 87

Answers (1)

asthasr
asthasr

Reputation: 9427

With the definition of the token_dictionary, you're not defining a dictionary at all -- you've written the literal syntax for a set. You need to specify values for it to be a dictionary. I expect that if you change to using token_dictionary = {'TK_TYPE': None, 'TK_VALUE': None} you'll have more luck.

Also note that using .get() is unnecessary for retrieving a value from the dictionary. Just use [].

Upvotes: 2

Related Questions