473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

A strange behavior of list.extend()

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
Jul 18 '05 #1
2 7656
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.

Jul 18 '05 #2
Thanks. I got it.

Deahyok
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Robin Becker | last post by:
Is there some smart/fast way to flatten a level one list using the latest iterator/generator idioms. The problem arises in coneverting lists of (x,y) coordinates into a single list of...
20
by: SpreadTooThin | last post by:
I have a list and I need to do a custom sort on it... for example: a = #Although not necessarily in order def cmp(i,j): #to be defined in this thread. a.sort(cmp) print a
10
by: Drew | last post by:
I'm looking to add an element to list of items, however I'd like to add it at a specific index greater than the current size: list = list.insert(10,4) What I'd like to see is something like:
8
by: Su Y | last post by:
hi all, I can't understand how this code work, its behavior is really weird for me... I want find the first number in extend which is larger than num, so I wrote: def find(num): count=0 for...
5
by: Randy | last post by:
Hi. I am learning Design Patterns so I pulled this piece of code off the ng (decorator) . Now I am going through it with the debugger and seeing what does what and I have found something I just...
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
1
by: Tzury Bar Yochay | last post by:
while I can invoke methods of empty string '' right in typing (''.join(), etc.) I can't do the same with empty list example: I would not use b = a since I don't want changes on 'b' to...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.