A strange behavior of list.extend()

S

sdhyok

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

Daehyok
 
M

mackstann

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:
[1, 2, 3, 4]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top