How to store the reference of a dictionary element ?

Terry Hancock hancock at anansispaceworks.com
Mon Dec 16 21:37:40 EST 2002


On Monday 16 December 2002 06:06 pm,   Alfredo P. Ricafort wrote:
> 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}
> >>> 

Try this:

>>> a = {'1':[1], '2':[2]}
>>> 
>>> b = {}
>>> b['2']=a['2']
>>> a['2'][0]= 22
>>> b
{'2': [22]}

can that do what you need?

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

"Some things are too important to be taken seriously"




More information about the Python-list mailing list