Looking for a different version of sort

Brian McGonigle BigsyNY at yahoo.com
Wed Jun 16 04:19:45 CEST 2004


I'm a Perl programmer learning Python (up to chapter 7 in Learning
Python, so go easy on me :-) and I find that I look to do things in
Python the way I would do them in Perl. In Perl functions and methods
usually only return and undefined value in the event of an error, make
an endless number of compound statements possible. Is there a version
of sort() I could import from somewhere that returns a reference to
the object on which it was performed, rather than returning "None".

>>> t = ('x','y','z','a','b','c',)
>>> t
('x', 'y', 'z', 'a', 'b', 'c')
>>> list(t)
['x', 'y', 'z', 'a', 'b', 'c']
>>> l = list(t).sort()
>>> print l
None
>>> l = list(t)
>>> l.sort()
>>> l
['a', 'b', 'c', 'x', 'y', 'z']
>>>

I would like "list(t).sort()" to return a new sorted list. For
example, then I could do "t = tuple(list(t).sort())" to simulate an
in-place sort of a tuple assigned to the variable "t". When I try this
now I get:

>>> t = tuple(list(t).sort())
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence
>>>

I assume this is because the "None" returned from sort() is the
non-sequence, but if sort had returned a reference to the list object
it was called upon, it would work.

Thanks,
Brian



More information about the Python-list mailing list