fork download
  1. fruits = ['watermelon', 'orange', 'pear', 'mango', 'apple', 'grapes']
  2. values = [3, 5, 2, 3, 2, 4]
  3. firsts = ['grapes', 'orange', 'apple']
  4.  
  5. mapping = dict(zip(fruits, values))
  6. index = dict(map(reversed, enumerate(firsts)))
  7. new_fruits = sorted(fruits, key=lambda k: (index.get(k, float('inf')), k))
  8. new_values = list(map(mapping.get, new_fruits))
  9.  
  10. print(new_fruits)
  11. print(new_values)
Success #stdin #stdout 0.03s 9568KB
stdin
Standard input is empty
stdout
['grapes', 'orange', 'apple', 'mango', 'pear', 'watermelon']
[4, 5, 2, 3, 2, 3]