Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hi, I am getting the following errorTypeError: sort() takes no keyword arguments

3,269 views
Skip to first unread message

gaurav kashyap

unread,
Oct 17, 2008, 12:53:10 AM10/17/08
to
Hi all,
I am using python version 2.3.in a program ,
I have called the sort function.Wherein,
a.sort(reverse=True)
is giving the following error:

TypeError: sort() takes no keyword arguments.

It works in python 2.4,What can be the alternative in python 2.3

Thanks ,
Gaurav

Aidan

unread,
Oct 17, 2008, 2:43:32 AM10/17/08
to

You could try:

>>> a.sort()
>>> a = a[::-1]

Peter Otten

unread,
Oct 17, 2008, 3:46:49 AM10/17/08
to
gaurav kashyap wrote:

a.reverse()
a.sort()
a.reverse()

gives the same result. If you don't care about the order of equal items

a.sort()
a.reverse()

will do.

Peter

Duncan Booth

unread,
Oct 17, 2008, 3:54:00 AM10/17/08
to
Aidan <awe...@gmail.com> wrote:

You could, but you should also be aware that there is a subtle difference.
The 'reverse' keyword argument gives a stable reverse sort, sorting and
then reversing something is not stable. e.g.

>>> data = [(1, 'zzz'), (1, 'abc'), (2, 'def'), (1, 'pqr'), (2, 'jkl')]
>>> a = data[:]
>>> a.sort(lambda p,q: cmp(p[0],q[0]), reverse=True)
>>> a
[(2, 'def'), (2, 'jkl'), (1, 'zzz'), (1, 'abc'), (1, 'pqr')]
>>> a = data[:]
>>> a.sort(lambda p,q: cmp(p[0],q[0]))
>>> a[::-1]
[(2, 'jkl'), (2, 'def'), (1, 'pqr'), (1, 'abc'), (1, 'zzz')]


--
Duncan Booth http://kupuguy.blogspot.com

0 new messages