# [Python-Dev] Re: lists v. tuples

Christian Tismer tismer@tismer.com
Sat, 15 Mar 2003 16:59:19 +0100

```Alex Martelli wrote:
> On Friday 14 March 2003 04:42 pm, Christian Tismer wrote:
...

>>And the key assumption for sorting things is that
>>the things are sortable, which means there
>>exists and order on the basic set.
>>Which again suggests that list elements usually
>>have something in common.
>
> If a list contains ONE complex number and no other number,
> then the list can be sorted.

By a similar argument, tuples of one element can be sorted
and reversed, just by doing nothing :-)

> If the list contains elements that having something in common,
> by both being complex numbers, then it cannot be sorted.

Sure it can, by supplying a compare function, which implements
the particular sorting operation that you want. Perhaps you
want to sort them by their abs value or something. (And then
you probably will want a stable sort, which is meanwhile
a nice fact thanks to Tim:

>>> a=[1, 2, 2+2j, 3+1j, 1+3j, 3-3j, 3+1j, 1+3j]
>>> a.sort(lambda x, y:cmp(abs(x), abs(y)))
>>> a
[1, 2, (2+2j), (3+1j), (1+3j), (3+1j), (1+3j), (3-3j)]
>>>

)

Complex just has no total order, which makes it impossible to
provide a meaningful default ordering.

> So, lists whose elements have LESS in common (by being of
> widely different types) are more likely to be sortable than lists
> some of whose elements have in common the fact of being
> numbers (if one or more of those numbers are complex).

I agree that my statement does not apply when putting
non-sortable things into a list. But I don't believe
that people are putting widely different types into
a list in order to sort them. (Although there is an
arbitrary order between strings and numbers, which
I would drop in Python 2.4, too).

chris
--
Christian Tismer             :^)   <mailto:tismer@tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  pager +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today?   http://www.stackless.com/

```