append method

Jean-Michel Pichavant jeanmichel at sequans.com
Wed May 23 10:26:25 EDT 2012


水静流深 wrote:
> >>> s=[1,2,3]
> >>> s.append(5)
> >>> s
> [1, 2, 3, 5]
> >>> s=s.append(5)
> >>> s
> >>> print s
> None
>
> why can't  s=s.append(5)  ,what is the reason?
Because the append method returns None, not the object. It modifies the 
object in place, and does not create any copy.

You can still write
s = s + [5]
if you really want to, but what's the point ?

JM






More information about the Python-list mailing list