0

I have two lists:

refList = ['INDIA', 'ALPHA','CHARLIE','BRAVO','WHISKEY','SIERRA']
sortList = ['CHARLIE','SIERRA','ALPHA', 'INDIA']

I want to sort sortList according to refList, i.e.:

result = ['INDIA', 'ALPHA','CHARLIE','SIERRA']

What is the most efficient way of doing this?

RomanHotsiy
  • 4,978
  • 1
  • 25
  • 36
jm8741
  • 3
  • 1
  • I did search for and try that method in the link you shared, but itdid not work for me. If you notice in that question, the two source lists are different in terms of elements within them.. X is an alphabetical list and Y is a numeric list. – jm8741 Jul 22 '14 at 15:10

1 Answers1

1
>>> nlist = [r for r in refList if r in sortList]
>>> print nlist
['INDIA', 'ALPHA', 'CHARLIE', 'SIERRA']

May not be the most efficient way since we search in my list for every element in reflist. This is just simple and good enough for small lists

Raul Guiu
  • 2,374
  • 22
  • 37