When it is required to sort the list of tuples in a customized manner, the 'sort' method can be used.
The 'sort' method sorts the elements of the iterable in a specific order, i.e ascending or descending. It sorts the iterable in-place.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list.
Below is a demonstration of the same −
def tuple_sort(my_tup): my_tup.sort(key = lambda x: x) return my_tup my_tuple = [('Will', 100), ('John', 67), ('Harold', 86), ('Jane', 35)] print("The tuple is ") print(my_tuple) print("The sorted list of tuple is :") print(tuple_sort(my_tuple))
The tuple is [('Will', 100), ('John', 67), ('Harold', 86), ('Jane', 35)] The sorted list of tuple is : [('Jane', 35), ('John', 67), ('Harold', 86), ('Will', 100)]