sort list by another list python code example
Example 1: sort a list by values of another one python
numbers = [1, 2, 3]
letters = ['c', 'a', 'b']
sorted_list = [number for letter, number in sorted(zip(letters, numbers))]
print(sorted_list)
Example 2: sort two lists by one python
list1 = [3,2,4,1,1]
list2 = ['three', 'two', 'four', 'one', 'one2']
list1, list2 = zip(*sorted(zip(list1, list2)))
Example 3: sort list with respect to another list
list1 = [[1,1],[1,2],[1],[2,2]]
list2 = [2, 3, 1, 4]
zipped_lists = zip(list1, list2)
sorted_pairs = sorted(zipped_lists)
tuples = zip(*sorted_pairs)
c,v = [ list(tuple) for tuple in tuples]
print(c)