[Tutor] List methods inside a dictionary of list
Rafael Turner
steven.rafael.turner at gmail.com
Mon Jul 11 15:26:50 CEST 2011
Hello,
I am playing lists and dictionaries and I came across this
counter-intuitive result.
>>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]]))
>>>d
Out:
{'a': [0],
'b': [0],
'c': [0],
'd': [0],
'e': [0],
'g': [0],
'j': [0],
'q': [0]}
>>> d['a'].__setitem__(0,4)
>>> d
Out:
{'a': [4],
'b': [4],
'c': [4],
'd': [4],
'e': [4],
'g': [4],
'j': [4],
'q': [4]}
I was not expecting all the keys to be updated. Is there any
documentation I could read on how different datatypes' methods and
operators interact differently when inside a dictionary? I would also
like to find a way of being able to use list methods in side a
dictionary so that
>>> d['a'][0] = 5
Does not return
{'a': [5],
'b': [5],
'c': [5],
'd': [5],
'e': [5],
'g': [5],
'j': [5],
'q': [5]}
But rather
{'a': [5],
'b': [0],
'c': [0],
'd': [0],
'e': [0],
'g': [0],
'j': [0],
'q': [0]}
Where d is made by d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g',
'j'],8*[[0]]))
Thanks a bunch,
Rafael
More information about the Tutor
mailing list