creating an artificial "last element" in sort list

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 28 22:05:15 EDT 2012


On Fri, 28 Sep 2012 16:39:33 -0700, dave wrote:

> a = ['a', 'b', x]
> b = sorted(a)
>
> What does x need to be to always be last on an ascending sort no matter
> what 'a' and 'b' are.... within reason... 

How about this?

a = ['a', 'b']
b = sorted(a) + ['whatever you want']

You could also do this:

x = max(a)
a.append(x)
b = sorted(a)


> I am expecting 'a' and 'b'
> will be not longer than 10 char's long.... I tried making x =
> 'zzzzzzzzzzzzzzzz' and believe it or not, this appears FIRST on the
> sort!!!

I think you are mistaken.

py> sorted(['a', 'b', 'zzzzzzzzzzzzzzzz'])
['a', 'b', 'zzzzzzzzzzzzzzzz']



But really, I don't understand what problem you are trying to solve. 
Perhaps if you explain the purpose of this, we can suggest a solution.


-- 
Steven



More information about the Python-list mailing list