Python - Sort list according to other list order Last Updated : 10 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Sorting a list based on the order defined by another list is a common requirement in Python, especially in scenarios involving custom sorting logic. This ensures that elements in the first list appear in the exact order specified in the second list. Python provides various methods to accomplish this.Using sorted()The sorted() function with key parameter allows sorting based on the index of elements in the reference list. This method is efficient and avoids manual looping. Python a = ['Python', 'with', 'GFG', 'Learn'] order = ['Learn', 'Python', 'with', 'GFG'] # Sort list 'a' based on 'order' sorted_list = sorted(a, key=lambda x: order.index(x)) print(sorted_list) Output['Learn', 'Python', 'with', 'GFG'] Explanation:sorted() iterates over each element in a and uses the index of the element in order to determine its position.Let's see some other methods to sort a list according to other list in PythonTable of ContentUsing Dictionary MappingUsing LoopsUsing Dictionary MappingA dictionary can map elements in a to their corresponding positions in order. Sorting then uses these positions for arrangement. Sorting is performed using dictionary lookups which are faster than list indexing. Python a = ['with', 'GFG', 'Learn', 'Python'] order = ['Learn', 'Python', 'with', 'GFG'] # Create dictionary to map 'order' to their indices order_dict = {value: index for index, value in enumerate(order)} # Sort 'a' based on 'order' using the dictionary sorted_list = sorted(a, key=lambda x: order_dict[x]) print(sorted_list) Output['Learn', 'Python', 'with', 'GFG'] Using LoopsManual looping can also achieve sorting but is less efficient and more verbose. The loop iterates through order and appends matching elements from a. Python a = ['with', 'Python', 'Learn', 'GFG'] order = ['Learn', 'Python', 'with', 'GFG'] # Sort manually sorted_list = [] for item in order: if item in a: sorted_list.append(item) print(sorted_list) Output['Learn', 'Python', 'with', 'GFG'] Comment More infoAdvertise with us Next Article Python - Sort list according to other list order M manjeet_04 Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads Python - Rearrange List by Other List Order We are given a list we need to rearrange list by other list order. For example, d = ['a', 'b', 'c', 'd'] and other list o = [2, 0, 3, 1] we need to rearrange this by other list so that resultant output should be ['c', 'a', 'd', 'b'].Using List ComprehensionList comprehension allows us to reorder ele 2 min read Python | Sort Flatten list of list The flattening of list of lists has been discussed earlier, but sometimes, in addition to flattening, it is also required to get the string in a sorted manner. Let's discuss certain ways in which this can be done. Method #1 : Using sorted() + list comprehension This idea is similar to flattening a l 7 min read Python - Sort list of Single Item dictionaries according to custom ordering Given single item dictionaries list and keys ordering list, perform sort of dictionary according to custom keys. Input : test_list1 = [{'is' : 4}, {"Gfg" : 10}, {"Best" : 1}], test_list2 = ["Gfg", "is", "Best"] Output : [{'Gfg': 10}, {'is': 4}, {'Best': 1}] Explanation : By list ordering, dictionari 4 min read Convert List of String into Sorted List of Integer - Python We are given a list of string a = ['3', '1', '4', '1', '5'] we need to convert all the given string data inside the list into int and sort them into ascending order so the output list becomes a = [1, 1, 3, 4, 5]. This can be achieved by first converting each string to an integer and then sorting the 3 min read Python - Sort List items on basis of their Digits Given List of elements, perform sorting on basis of digits of numbers. Input : test_list = [434, 211, 12, 3] Output : [12, 211, 3, 434] Explanation : 3 < 12, still later in list, as initial digit, 1 < 3. Hence sorted by digits rather than number. Input : test_list = [534, 211, 12, 7] Output : 2 min read Like