How to sort list

wittempj@hotmail.com martin.witte at gmail.com
Tue Nov 21 15:24:33 EST 2006



On Nov 21, 8:15 pm, "Lad" <pyt... at hope.cz> wrote:
> I have a list of emails and I would like to sorted that list by domains
> E.g.
> If the list is
>
> Emails=['... at hotmail.com','... at yahoo.com','... at hotmail.com','... at yahoo.com',....]
>
> after sorting I would like to have
> Emails=['... at hotmail.com','... at hotmail.com','... at yahoo.com','... at yahoo.com',....]
>
> What is the best/easiest way?
>
> Thank you for help
> L.

Another way would be a compare function as callback function in the
sort() method of list:

py>def compare(a, b):
py>    a = a.split('@')[1]
py>    b = b.split('@')[1]
py>    return cmp(a, b)
py>
py>Emails =
['... at hotmail.com','... at yahoo.com','... at hotmail.com','... at yahoo.com']
py>Emails.sort(compare)
py>print Emails
['... at hotmail.com', '... at hotmail.com', '... at yahoo.com', '... at yahoo.com']




More information about the Python-list mailing list