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.
Python3
# 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.
Python3
# 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
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: Python3 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 Python3 tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Syntax
3 min read
Remove empty tuples from a list - Python
The task of removing empty tuples from a list in Python involves filtering out tuples that contain no elements i.e empty. For example, given a list like [(1, 2), (), (3, 4), (), (5,)], the goal is to remove the empty tuples () and return a new list containing only non-empty tuples: [(1, 2), (3, 4),
3 min read
Python - Reversing a Tuple
We are given a tuple and our task is to reverse whole tuple. For example, tuple t = (1, 2, 3, 4, 5) so after reversing the tuple the resultant output should be (5, 4, 3, 2, 1).Using SlicingMost common and Pythonic way to reverse a tuple is by using slicing with a step of -1, here is how we can do it
2 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
The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read