1

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?

Community
  • 1
  • 1
A. Ghelardi
  • 43
  • 1
  • 7
  • 1
    "is it possible, or am I trying to use the wrong solution to a common problem (very probable)?" You are actually *creating* a problem that doesn't necessarily exist. Why not reference `str3` as `my_dict['C']`? – DeepSpace Feb 26 '17 at 15:08
  • This will probably help you out: https://nedbatchelder.com/text/names.html – jonrsharpe Feb 26 '17 at 15:09
  • @DeepSpace sorry, I do not fully understand what you mean. Link provided by jonrshapre helps me understand why my approach was *not* working. – A. Ghelardi Feb 26 '17 at 18:01

2 Answers2

0

You need to put mutable variables into your dictionary.

str3 = ['banana']
my_dict = {'A':str1, 'B':str2, 'C':str3, '1':[5], '2':[9], '3':[13]}

my_dict['C'][0] = 'apple'
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Marking as solution as this is the approach to the specific problem on how to change variables inside a dictionary. They must be mutable. Regarding my specific problem, after managing the variable I will then need to convert it back from list to string. – A. Ghelardi Feb 26 '17 at 17:57
0

First, there's probably a better way to achieve what you want to do.

Then, I don't think you can prevent variables from being interpreted at dict instantiation.

You could create a custom MyVar class, though :

class MyVar:

    def __init__(self, content):
        self.content = content

    def update(self, new_content):
        self.content = new_content

    def __repr__(self):
        return self.content


def update_value(my_dict, key, new_content):
    value = my_dict.get(key)
    if isinstance(value, MyVar):
        value.update(new_content)
    else:
        my_dict[key] = new_content

str1 = MyVar('abcdefg')
str2 = MyVar('duck')
str3 = MyVar('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}

update_value(my_dict, 'C', 'apple')
update_value(my_dict, '2', 11)
update_value(my_dict, 'D', 4)

print my_dict
# {'A': abcdefg, 'C': apple, 'B': duck, 'D': 4, '1': 5, '3': 13, '2': 11}
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124