sorting two corresponding lists?

Esmail ebonak at hotmail.com
Tue Apr 21 07:58:48 EDT 2009


Esmail wrote:
> 
> items = [apple, car, town, phone]
> values = [5, 2, 7, 1]
> 
> I would like to sort the 'items' list based on the 'values' list so
> that I end up with the following two list:
> 
> items = [town, apple, car, phone]
> values = [7, 5, 2, 1]

Hello all,

thanks for all the great suggestions. I used Diez's post as an
opportunity to learn about zip and this this is the code I
ended up drafting:

   li=zip(*sorted(zip(values, items), reverse=True))
   new_items=list(li[1])
   new_vals=list(li[0])

seems to work :-)

Esmail




More information about the Python-list mailing list