dict tuple update

Greg Fortune lists at gregfortune.com
Tue May 6 00:50:20 EDT 2003


Actually, it's just because tuples are immutable (cannot be modified).  
Change you tuples to lists and everything will work fine :)

> d = {
>    0 : ["title0", "", ""],
>    1 : ["title1", "", None, None, None],
>    2 : ["title2", "song2", "track1"],
>    3 : [ None, None, "track0"],
>    4 : ["title4", "song3", None],
> }

Greg


Tertius wrote:

> # I  have a dict() type defined like this
> 
> d = {
>    0 : ("title0", "", ""),
>    1 : ("title1", "", None, None, None),
>    2 : ("title2", "song2", "track1"),
>    3 : ( None, None, "track0"),
>    4 : ("title4", "song3", None),
> }
> 
> # I need to change all None values in the tuple values to "" (empty
> # string) Errors happen when trying to update the tuple. I presume its #
> # because a dict value is referenced. I tried copy and deepcopy but no #
> 
# luck.
> #     t[j] = ""
> # TypeError: object doesn't support item assignment
> 
> 
> import copy
> t = tuple()
> for i in d:
>      for j in range(len(d[i])):
>          if d[i][j] is None:
>              print d[i]
>              t = copy.copy(d[i])
>              t[j] = ""
>              x = {i:t}
>              d.update(x)
> 
> 
> What am I doing wrong?
> 
> Thanks,
> Tertius





More information about the Python-list mailing list