Sorting email addresses by domain

Peter Murray pdmurray at ntlworld.com
Sun Nov 14 06:48:00 EST 2004


Alex Martelli wrote:

> I'd prefer DSU...:
> 
> def key(addr): return addr.split('@', 1)[::-1]
> 
> aux = [ (key(a), a) for a in addresses ]
> aux.sort()
> addresses[:] = [ x for __, x in aux ]

I hadn't appreciated reversing a sequence by using a slice with a step of -1
(to be honest, I hadn't remembered about the step at all! I am a newbie :-)

Interesting though that to get the whole sequence reversed, you have to use
seq[::-1], as if you actually specify the bounds, you would always miss out
the final item:

>>> "peter murray"[::-1]
'yarrum retep'
>>> "peter murray"[12:0:-1]
'yarrum rete'
>>> "peter murray"[12:-1:-1]
''

Because the slice is up to and not including the final item.

Sorry if this post is out of the thread, the NG is very bare on my news
server so I'm posting to the mailing list.

Pete.







More information about the Python-list mailing list