Linux Hint Posted June 28, 2023 Share Posted June 28, 2023 The “Sorting” operation involves arranging a collection of items/elements in a certain order. In Python, there are many ways to sort different types of data structures, such as lists, dictionaries, sets, and tuples. “Tuples” are immutable sequences of values that can store heterogeneous data. Tuples often represent records or structured data with a fixed/specific number of fields and a fixed order. This write-up will guide you to sort the list of tuples using relevant examples. How to Sort a Python List of Tuples? To sort the specified list of tuples, the below two methods are used in Python: “sorted()” Function. “list.sort()” Method. Method 1: Sort a List of Tuples Using the “sorted()” Function In Python, the “sorted()” function is used to sort the initialized list in ascending and descending orders. The “sorted()” method can also be utilized to sort/arrange the list of tuples. Syntax sorted(iterable, key, reverse) In the above syntax: The “iterable” parameter indicates the sequence/iterable that needs to be sorted. The “key” parameter specifies the function that decides the order of the sorting operation. The “reverse” parameter specifies whether the input list is sorted in ascending (when “True”) or descending order (when “False”). Example 1: Sort a List of Tuples Using the Default Parameter This example code is implemented to sort the Python list of tuples: list_tuple1 = [(10, 12), (22, 42), (25, 30), (15, 10)] print('List of Tuple:', list_tuple1) sorted_tuple = sorted(list_tuple1) print('\nSorted Tuple Value: ',sorted_tuple) In the above code: The list of tuples is initialized and displayed on the console. The “sorted()” function accepts the list of tuples as an argument and sorts the list based on the first item/element of the tuple in ascending order. Output The input list of tuples has been sorted in ascending order based on the first tuple elements i.e., 10, 15, 22, 25. Example 2: Sort a List of Tuples Using the “Key” Parameter This example code sorts the list of tuples by utilizing the “key” parameter: list_tuple1 = [(10, 12), (22, 42), (25, 30), (15, 10)] print('List of Tuple:', list_tuple1) sorted_tuple = sorted(list_tuple1, key=lambda i:i[1]) print('\nSorted Tuple Value: ',sorted_tuple) According to the above code: The “sorted()” function takes the list of tuples and keys as its argument and parameter, respectively, and retrieves the sorted list of tuples according to the second item/element of the tuple. Also, the “key” value takes the “lambda” function that determines the value utilized for sorting. Output The defined list has been sorted into ascending order based on the second tuple element. Example 3: Sort a List of Tuples Using the “reverse” Parameter The below code is used to sort the list of tuples utilizing the “reverse” parameter: list_tuple1 = [(10, 12), (22, 42), (25, 30), (15, 10)] print('List of Tuple:', list_tuple1) sorted_tuple = sorted(list_tuple1, key=lambda i:i[1], reverse=True) print('\nSorted Tuple Value: ',sorted_tuple) In the above code, the parameter “reverse=True” is passed to the “sorted()” function that sorts/arrange the Python list of tuples according to the second item/element of the tuple in descending order. Output The defined list has been sorted into descending order based on the second tuple element value appropriately. Method 2: Sort List of Tuples Using the “list.sort()” Method The “sort()” method sorts/arrange the input list in ascending (by default) and descending orders. This method can also be employed to sort the Python tuples list. Syntax list.sort(key=..., reverse=...) In the above syntax, the optional “key” and “reverse” parameters indicate the sorting criteria and sort the list in ascending and descending order based on the Boolean value. Example: Sort a List of Tuples The following example code is used to sort a list of tuples via the “list.sort()” method: list_tuple1 = [(55, 12), (22, 42), (25, 30), (15, 10)] print('List of Tuple:', list_tuple1) list_tuple1.sort() print('\nSorted Tuple Value: ',list_tuple1) In the above code lines: The list of tuples “list_tuple1” is initialized in the code. The “sort()” method is utilized to sort/arrange the list of tuples based on the first tuple element in ascending order. Output According to the first tuple element of the input list, the inputs are sorted in ascending order. Conclusion The “sorted()” function or the “list.sort()” method is used to sort the list of tuples in increasing/ascending and descending/decreasing orders or based on the “key” value. These approaches accept the “key” parameter and sort the list of tuples based on that value. Similarly, the “reverse” parameter value is utilized to sort the particular list of tuples in ascending or descending order. This Python write-up provided a comprehensive tutorial on sorting the list of tuples using numerous examples. View the full article Quote Link to comment Share on other sites More sharing options...

## Recommended Posts

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.