444,035 Members | 1,408 Online
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

# Sorting part of a list

 P: n/a Hello, I thought I understood list slices, but I don't. I want to sort only the last part of a list, preferably in place. If I do ll = [3, 1, 4, 2] ll[2:].sort() ll [3, 1, 4, 2] ll isn't changed, because ll[2:] is a copy of the last part of the list, and this copy is sorted, not the original list. Right so far? But assignment to the slice: ll[2:] = [2, 4] ll [3, 1, 2, 4] _does_ change my original ll. What did I misunderstand? Thanks, Sibylle -- Dr. Sibylle Koczian Universitaetsbibliothek, Abt. Naturwiss. D-86135 Augsburg e-mail : Si*************@Bibliothek.Uni-Augsburg.DE Jul 19 '05 #1
5 Replies

 P: n/a You can assign to a slice in place, but referencing a slice creates a copy. I think you understand it quite well :-) a = ll[2:] a.sort() ll[2:] = a To do a partial sort, in place, you'll have to subclass list - but you'll still end up doing something similar unless you delve into C or write your own sort algorithm in Python. Neither are recommended. Best Regards, Fuzzy http://wwww.voidspace.org.uk/python (back online hurrah) Jul 19 '05 #2

 P: n/a Sibylle Koczian wrote: Hello, I thought I understood list slices, but I don't. I want to sort only the last part of a list, preferably in place. If I do >>> ll = [3, 1, 4, 2] >>> ll[2:].sort() It may help in unravelling any bogglement to point out that this is equivalent to temp = ll[2:]; temp.sort(); del temp >>> ll [3, 1, 4, 2] ll isn't changed, because ll[2:] is a copy of the last part of the list, and this copy is sorted, not the original list. Right so far? Quite correct. But assignment to the slice: >>> ll[2:] = [2, 4] >>> ll [3, 1, 2, 4] _does_ change my original ll. Quite correct. What did I misunderstand? What misunderstanding? You have described the behaviour rather precisely. Which of the two cases is boggling you? Jul 19 '05 #3

 P: n/a Fuzzyman wrote: a = ll[2:] a.sort() ll[2:] = a To do a partial sort, in place, you'll have to subclass list Or be using 2.4: ll = [3, 1, 4, 2] ll[2:] = sorted(ll[2:]) ll [3, 1, 2, 4] -- Benji York Jul 19 '05 #4

 P: n/a On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian declaimed the following in comp.lang.python: _does_ change my original ll. What did I misunderstand? ll[2:].sort() breaks down to: newList = ll[2:] newList.sort() The slice is a "take" from the right hand side of an implied assignment (create new object, sort it) ll[2:] = ... is not an object creation, merely an access into an existing object. ll = [3, 1, 4, 2] nl = ll[2:] # nl <- newList nl.sort() ll[2:] = nl ll [3, 1, 2, 4] I'm still running 2.3.x -- did "sorted()" make it into 2.4? As I recall the discussion, the idea was to have a sort operation that would return the sorted data, instead of the current in-place sort and return of None. That would allow for: ll[2:] = ll[2:].sorted() -- ================================================== ============ < wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < wu******@dm.net | Bestiaria Support Staff < ================================================== ============ < Home Page: < Overflow Page: < Jul 19 '05 #5

 P: n/a Dennis Lee Bieber schrieb: On Fri, 24 Jun 2005 13:42:39 +0200, Sibylle Koczian declaimed the following in comp.lang.python: ll[2:] = ... is not an object creation, merely an access into an existing object. That's what I hadn't understood. Although it's the same with ll[2]. I'm still running 2.3.x -- did "sorted()" make it into 2.4? As I recall the discussion, the idea was to have a sort operation that would return the sorted data, instead of the current in-place sort and return of None. That would allow for: ll[2:] = ll[2:].sorted() It's not a list method, but it's a function: ll = [3, 1, 4, 2] ll[2:] = ll[2:].sorted() Traceback (most recent call last): File "", line 1, in -toplevel- ll[2:] = ll[2:].sorted() AttributeError: 'list' object has no attribute 'sorted' but this works: ll = [3, 1, 4, 2] ll[2:] = sorted(ll[2:]) ll [3, 1, 2, 4] I'm using 2.4, but didn't look carefully at the changes. Thanks to all who answered! Sibylle -- Dr. Sibylle Koczian Universitaetsbibliothek, Abt. Naturwiss. D-86135 Augsburg e-mail : Si*************@Bibliothek.Uni-Augsburg.DE Jul 19 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.