A strange behavior of list.extend()

mackstann mack at incise.org
Sun Aug 24 17:29:44 EDT 2003


On Sun, Aug 24, 2003 at 02:11:15PM -0700, sdhyok wrote:
> I think the two script should produce the same results, but not.
> Why?
> 
> ---------------- Script #1
> ls = [1]
> ls.extend([2,3])
> print ls
> -> [1,2,3]
> 
> --------------- Script #2
> ls = [1].extend([2,3])
> print ls
> -> None

list.extend happens in place and returns None, so your ls variable is
just getting the returned None.  Same thing happens with sort:

>>> [4,2,3,1].sort()
>>> 

or:

>>> f = [4,2,3,1]       
>>> f.sort()
>>> f
[1, 2, 3, 4]


-- 
m a c k s t a n n  mack @ incise.org  http://incise.org
Excellent time to become a missing person.





More information about the Python-list mailing list