Python | Sort a tuple by its float element
Last Updated :
24 Dec, 2017
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:
- Iterable : sequence (list, tuple, string) or collection (dictionary, set, frozenset) or any other iterator that needs to be sorted.
- Key(optional) : A function that would server as a key or a basis of sort comparison.
- 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.
def Sort(tup):
return(sorted(tup, key = lambda x: float(x[1]), reverse = True))
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.
def Sort(tup):
tup.sort(key = lambda x: float(x[1]), reverse = True)
print(tup)
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
Similar Reads
Python Tuple Methods
Python Tuples is an immutable collection of that are more like lists. Python Provides a couple of methods to work with tuples. In this article, we will discuss these two methods in detail with the help of some examples. Count() MethodThe count() method of Tuple returns the number of times the given
3 min read
Python Tuple - max() Method
While working with tuples many times we need to find the maximum element in the tuple, and for this, we can also use max(). In this article, we will learn about the max() method used for tuples in Python. Example Tuple =( 4, 2, 5, 6, 7, 5) Input: max(Tuple) Output: 7 Explanation: The max() method re
2 min read
Python Tuple - min() Method
While working with tuples many times we need to find the minimum element in the tuple, and for this, we can also use min(). In this article, we will learn about the min() method used for tuples in Python. Syntax of Tuple min() MethodSyntax: min(object) Parameters: object: Any iterable like Tuple, Li
2 min read
Python Tuple - index() Method
While working with tuples many times we need to access elements at a certain index but for that, we need to know where exactly is that element, and here comes the use of the index() function. In this article, we will learn about the index() function used for tuples in Python. Example : The Index() m
2 min read
Python Tuple - len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: C/C++ Code Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(len(Tuple
2 min read
Python Tuple count() Method
In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example C/C++ Code tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Synt
3 min read
Remove empty tuples from a list - Python
In this article, we will explore various method to remove empty tuples from a list. The simplest method is by using a for loop. Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the tuple is not empty then add it to the result list. [GFGTABS] Python
2 min read
Python | Reversing a Tuple
As we know that in Python, tuples are immutable, thus it cannot be changed or altered. This provides us with limited ways of reversing a tuple, unlike a list. We will go through few techniques on how a tuple in python can be reversed. Examples: Input : tuples = ('z','a','d','f','g','e','e','k') Outp
3 min read
Python - Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read
Create a List of Tuples in Python
List of tuple is used to store multiple tuples together into List. We can create a list that contains tuples as elements. This practice is useful for memory efficiency and data security as tuples are immutable. The simplest way to create a list of tuples is to define it manually by specifying the va
4 min read