Open In App

Python | Sort a tuple by its float element

Last Updated : 24 Dec, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting.

Examples:

Input : tuple = [('lucky', '18.265'), ('nikhil', '14.107'), 
                  ('akash', '24.541'), ('anand', '4.256'), ('gaurav', '10.365')]
Output : [('akash', '24.541'), ('lucky', '18.265'), 
          ('nikhil', '14.107'), ('gaurav', '10.365'), ('anand', '4.256')]

Input : tuple = [('234', '9.4'), ('543', '16.9'), ('756', '5.5'), 
                  ('132', '4.2'), ('342', '7.3')]
Output : [('543', '16.9'), ('234', '9.4'), ('342', '7.3'), 
          ('756', '5.5'), ('132', '4.2')]

We can understand this from the image shown below:

Method 1 : Use of sorted() method

Sorted() sorts a tuple and always returns a tuple with the elements in a sorted manner, without modifying the original sequence. It takes three parameters from which two are optional, here we tried to use all of the three:

  1. Iterable : sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
  2. Key(optional) : A function that would server as a key or a basis of sort comparison.
  3. Reverse(optional) : If set true, then the iterable would be sorted in reverse (descending) order, by default it is set as false.

To sort this in ascending order we could have just ignored the third parameter.




# Python code to sort the tuples using float element
# Function to sort using sorted()
def Sort(tup):
    # reverse = True (Sorts in Descending order)
    # key is set to sort using float elements
    # lambda has been used
    return(sorted(tup, key = lambda x: float(x[1]), reverse = True))
  
# Driver Code
tup = [('lucky', '18.265'), ('nikhil', '14.107'), ('akash', '24.541'), 
    ('anand', '4.256'), ('gaurav', '10.365')]
print(Sort(tup))


Output:

[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'),
 ('gaurav', '10.365'), ('anand', '4.256')]

Method 2 : In-place way of sorting using sort():

While sorting via this method the actual content of the tuple is changed, while in the previous method the content of the original tuple remained the same.




# Python code to sort the tuples using float element
# Inplace way to sort using sort()
def Sort(tup):
    # reverse = True (Sorts in Descending order)
    # key is set to sort using float elements
    # lambda has been used
    tup.sort(key = lambda x: float(x[1]), reverse = True)
    print(tup)
  
# Driver Code
tup = [('lucky', '18.265'), ('nikhil', '14.107'), ('akash', '24.541'), 
    ('anand', '4.256'), ('gaurav', '10.365')]
Sort(tup)


Output:

[('akash', '24.541'), ('lucky', '18.265'), ('nikhil', '14.107'), 
 ('gaurav', '10.365'), ('anand', '4.256')]

For more reference visit:
sorted() in Python
lambda in Python



Previous Article
Next Article

Similar Reads

Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Convert String float to float list in Python
Sometimes, while working with data, we could be dealing with numbers, that are in decimal and not integers. This is a general case in the data science domain. Let's discuss how to resolve a problem in which we may have a comma-separated float number and we need to convert it to a float list. Method #1 : Using list comprehension + split() + float()
4 min read
Python - Convert Float String List to Float Values
Sometimes, while working with Python Data, we can have a problem in which we need to perform conversion of Float Strings to float values. This kind of problem is quite common in all domains and find application quite often. Let's discuss certain ways in which this task can be performed. Input : test_list = ['8.6', '4.6'] Output : [8.6, 4.6] Input :
5 min read
Python | Convert tuple to float value
Sometimes, while working with tuple, we can have a problem in which, we need to convert a tuple to floating-point number in which first element represents integer part and next element represents a decimal part. Let's discuss certain way in which this can be achieved. Method : Using join() + float() + str() + generator expression The combination of
3 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg