Sorting a list to by another's order

Graeme Longman glongman at
Fri Aug 30 15:22:42 CEST 2002


I have two lists:

listA = ['d', 'c', 'f', 'a', 'b', 'e']
listB = ['a', 'c', 'd', 'f']

I need to sort listA so that it's order corresponds to that of listB (if
the item in listA isn't in listB then it should be at the end of the

Is there a quick way to do this using sort() instead of writng a bunch
of for loops and if-else statements ?

Thanks very much.

More information about the Python-list mailing list