0

I need to parse a dictionary and replace placeholder keys with other values. The dictionary is nested, the placeholder can appear multiple times and at different depths.

Therefore I have written a recursive function which traverses the dict and creates a list of values that belong to keys which are equal to the placeholder.

With this list, I would like to replace the values. This needs reference semantics. It doesn't work:

old={"token":4}
reference=old["token"]
reference=5
print(old["token"]==5)    #False

old={"token":4}
references=[]
references.append(old["token"])
references[0]=5
print(old["token"]==5)   #False

How can I create references to dictionary entries ?

lhk
  • 27,458
  • 30
  • 122
  • 201
  • *"How can I create references to dictionary entries ?"* Long story short: you can't; that's just not how Python works. – jonrsharpe Oct 22 '15 at 13:25
  • I may just be mis-interpreting this, but can't you use a for loop at each depth on the dict keys and match it? `if old.get('token') != None: old['token']=reference` or something of the like? I mean, you can't replace the values of a dict with a reference, but you can go through the dict and replace them. – Steven Summers Oct 22 '15 at 13:41

0 Answers0