How to store the reference of a dictionary element ?

Alfredo P. Ricafort alpot at mylinuxsite.com
Mon Dec 16 20:08:38 EST 2002


Hi,

How can I store the reference of a dictionary element in another
dictionary such that if I change the element of one it would be
reflected on the other? For example:

>>> a={'1':1,'2':2}
>>> b['2']=a['2']
>>> a['2']=22               <---- change a['2']
>>> a
{'1': 1, '2': 22}
>>> b                       <---- did not reflect the change in a['2']
{'2': 2}
>>> 

if I change a['2']=22 then b['2'] should equal 22 as well - but it
doesn't.


What I am actually trying to do is a bit more complicated than the
example above. I am trying to create something like a linked list so the
reference would be on the same dictionary, something like this:

>>> a={'1':1}
>>> a['2']=a['1']            <---- a['2'] should refer to a['1']
>>> a
{'1': 1, '2': 1}
>>> a['1']=11                <---- changed a['1']
>>> a['2']                   <---- a['2'] is not changed
1

Again, the problem is if I change a['1'], it would not be reflected in
a['2']. 

So how can I do this ?

Thanks.

AL









More information about the Python-list mailing list