As part of my exercise, I'm playing with dictionaries. I created some string variables, then I need to pass such vars to a dictionary as values (not as keys). Finally, I will need to update/replace such variables using dictionary keys!
However, as soon I create my_dict, variables are "exploded" and their content passed to dictionary as values. Instead, I wish to keep vars and dictionary values "linked"
This code is part of a function which is part of program which requests user inputs. These strings change quite a bit and referencing them using dictionary keys would greatly simplify my code.
str1 = 'abcdefg'
str2 = 'duck'
str3 = 'banana'
my_dict = {'A':str1, 'B':str2, 'C':str3, '1':5, '2':9, '3':13}
print my_dict
{'A': 'abcdefg', 'C': 'banana', 'B': 'duck', '1': 5, '3': 13, '2': 9}
my_dict['C']='apple'
print str3
banana # I wish str3 to become apple
print my_dict
{'A': 'abcdefg', 'C': 'apple', 'B': 'duck', '1': 5, '3': 13, '2': 9}
This question has some similarities to this one but my actual problem is that I wish to keep dictionary-values as variables and use their key to update variable content.
Is it possible, or am I trying to use the wrong solution to a common problem?