|
Hy everyone. I have a defined variable: And now, I want to update the value associated with 'apr2012' key appending 'cs382' string to ['cs101'] list. Then, dictionary would be something like this: If I run this statement: dictionary['apr2012']=dictionary['apr2012'].append('cs387'), then If I run dictionary['apr2012'].append('cs387') statement, then |
The question has been closed for the following reason "The question is answered, right answer was accepted" by Eduardo 28 Mar '12, 04:33
|
This happens because append mutates the structure. It does not return a new structure. So when you do
...you're saying "The value for the keyword 'apr2012' shall be whatever is returned from the append operation here". And append returns nothing, and therefore the result later is None.
by itself get out the list and appends a new value to it, that is it actually changes the list that is the value of the keyword 'apr2012'. The irony of the first version is that you're doing an append, which is what you want, but then you throw away the value you just mutated and replaces it with nothing :-) |