[Tutor] assigning list to keys
Christian Witts
cwitts at compuscan.co.za
Tue Jul 14 09:33:06 CEST 2009
Todd Matsumoto wrote:
> Hello,
>
> The other day I needed to pack a dictionary, the value of each key was a list. In the code I was packing the list and the dictionary at the same time. First I tried something like this:
>
> list = []
> dict = {}
> x = 1
>
> dict['int'] = list.append(x)
>
> The result was {'int': None}. Why is the value None?
>
> Cheers,
>
> T
>
Hi,
Appending to a list does so in-place and returns None. If you wish to
do what you posted you will need to do it in 2 steps with:
list.append(x)
dict['int'] = list
You should also avoid shadowing built-in names like "list", "dict",
"file" etc as you can very easily end up with unexplained errors.
Hope that helps.
--
Kind Regards,
Christian Witts
More information about the Tutor
mailing list