Python program to sort a tuple by its float element


This article will demonstrate how write a Python program to sort a tuple (made up of float elements) using its float elements. Here, we'll look at both how to sort using the in-built sorted() function and how to sort using the in-place method of sorting.

Input-Output Scenarios

Following is an input and its output scenario determining the sorting of a tuple by its float element −

Scenario-1

Input:
tuple = [(‘Dengu’, ’54.865’), (‘Malaria’, ‘345.743’), (‘Corona’, ‘456.864’), (‘Typhoid’, ‘35.285’), (‘Jaundice’, ’83.367’)]
Output:
[(‘Corona’, ‘456.864’), (‘Malaria’, ‘345.743’), (‘Jaundice’, ’83.367’), (‘Dengu’, ’54.865’), (‘Typhoid’, ‘35.285’)]

In the above scenario we can see that the tuples have been sorted in the descending order using its float elements.

Scenario-2

Input:
tuple = [(‘638’, ’54.865’), (‘932’, ‘345.743’), (‘256’, ‘456.864’), (‘843’, ‘35.285’), (‘246’, ’83.367’)]
Output:
[(‘256’, ‘456.864’), (‘932’, ‘345.743’), (‘246’, ’83.367’), (‘638’, ’54.865’), (‘843’, ‘35.285’)]

In the above scenario we can see that the tuple has been sorted in descending order by its float elements and not by its integer values.

Using sorted() method

Without changing the initial sequence, Sorted() sorts a tuple and always returns a tuple with the items in a sorted order. Here, we tried to use all three of the arguments i.e. iterable, key (optional) and reverse (optional), two of which are optional.

Algorithm

The following algorithm describes the ways to sort a tuple by its float element using sorted() method −

  • provided a list

  • using sorted to sort ().

  • Any iterator that needs to be sorted, whether it be a collection (dictionary, set, frozenset), sequence (list, tuple, string), or another type.

  • Using the function key(optional) as a basis for sort comparison would act as a key.

  • The iterable would be sorted in reverse (descending) order if Reverse (optional) were set to true. By default, it is set to false.

Example

Following is the code of Python for sorting a tuple by its float element using sorted() method −

def sort_tuple(X): return(sorted(X, key = lambda n: float(n[1]), reverse = True)) # The driver Code X = [('Dengu', '54.865'), ('Malari', '345.743'), ('Corona', '456.864'), ('Typhoi', '35.285'), ('Jaundice', '83.367')] print("Sorting of Tuples Using Its Float Element ::",sort_tuple(X))

Output

Following is an output of the above code −

Sorting of Tuples Using Its Float Element :: [('Corona', '456.864'), ('Malari', '345.743'), ('Jaundice', '83.367'), ('Dengu', '54.865'), ('Typhoi', '35.285')]

Using sort() method

The tuple's actual content is changed during this method of sorting, whereas the original tuple's content was left unchanged during the previous approach.

Algorithm

The following algorithm describes the ways to sort a tuple by its float element using sort() method −

  • Create a new Tuple List.
  • Define the Sort function (For Sorting Tuple).
  • Set the second element as the sorting key.
  • Use lambda sublist.
  • Print the outcome.

Example

In this code, using this method of sorting changes the tuple's actual contents. The sort() function sorts the elements of a list in ascending or descending order by using the default comparisons operator between items. Use the key argument to specify the function name to be used for comparison rather than the default operator −

def sort(tuple): # sorting in descending order by setting reverse as true using float elements tuple.sort(key = lambda x: float(x[1]), reverse = True) print(tuple) # The driver Code tuple = [('638', '54.865'), ('932', '345.743'), ('256', '456.864'), ('843', '35.285'), ('246', '83.367')] sort(tuple)

Output

Following is an output of the above code −

[('256', '456.864'), ('932', '345.743'), ('246', '83.367'), ('638', '54.865'), ('843', '35.285')]

Using Binary Search operation

In our code, we have a list of tuples that we must sort using the tuple's second member, the sorting index. We'll effectively use a sorting method, except we'll use the second element of the tuple rather than the first value from the list.

Algorithm

The following algorithm describes the ways to sort a tuple by the index of sorting by its float element using binary search operation −

  • List initialization.
  • Print the initial List.
  • Define the tuple list's length.
  • Using the binary function to sort.
  • Print the outcome

Example

The binary sorting method is used in this program to perform the sorting. The list will be sorted using an index determined by the second item of the tuple −

# Create a new tuple tuple_list = [('638', 54.865), ('932', 345.743), ('256', 456.864), ('843', 35.285), ('246', 83.367)] print("The orignal list is : ", str(tuple_list)) # Sort the list of tuples using the second item Len = len(tuple_list) for i in range(0, Len): for j in range(0, (Len - i - 1)): if(tuple_list[j][1] < tuple_list[j+1][1]): temp = tuple_list[j] tuple_list[j] = tuple_list[j+1] tuple_list[j+1] = temp print("The sorted list is : ", str(tuple_list))

Output

Following is an output of the above code −

The orignal list is :  [('638', 54.865), ('932', 345.743), ('256', 456.864), ('843', 35.285), ('246', 83.367)]
The sorted list is :  [('256', 456.864), ('932', 345.743), ('246', 83.367), ('638', 54.865), ('843', 35.285)]

Updated on: 23-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements