[Tutor] assigning list to keys
bob gailer
bgailer at gmail.com
Tue Jul 14 15:03:21 CEST 2009
Christian Witts wrote:
> 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
Don't you mean dict['int'] = list[-1]?
>
> 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.
>
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list