list behavior

Pettersen, Bjorn S BjornPettersen at fairisaac.com
Mon Oct 27 14:00:40 EST 2003


> From: anton muhin 
> 
> Rene Aguirre wrote:
[..]
> > Buen, when I just move the 'append' statement to the dictionary
> > assigment then:
> > 
> >>>>l = []
> >>>>d = {}
> >>>>d["one"] = l.append(1)
> >>>>d
> > 
> > {'one': None}
> > 
> > Not what I expected, then I came to the conclusion that
> > [].append(value) returns 'None', why?
> > 
> > Rene A.
> 
> append is what is called procedure in other languages.

Eh? (It's mutating the list in Python, and thus return None as all(?)
other mutatating methods).

> d["one"].append(1) should work

Nope, but

  d.setdefault('one', []).append(1)

will. (Not that I'm necessarily advocating it).

-- bjorn





More information about the Python-list mailing list