Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to sort a tuple by its float element
This article demonstrates how to sort a list of tuples by their float elements in Python. We'll explore three different approaches: using sorted(), using sort(), and implementing bubble sort manually.
Input-Output Scenarios
The following scenarios show how tuples are sorted by their float elements in descending order ?
Scenario 1
Input:
data = [('Dengue', '54.865'), ('Malaria', '345.743'), ('Corona', '456.864'), ('Typhoid', '35.285'), ('Jaundice', '83.367')]
Output:
[('Corona', '456.864'), ('Malaria', '345.743'), ('Jaundice', '83.367'), ('Dengue', '54.865'), ('Typhoid', '35.285')]
Scenario 2
Input:
data = [('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')]
Using sorted() Function
The sorted() function creates a new sorted list without modifying the original data. It accepts an iterable, an optional key function for custom sorting, and a reverse parameter ?
def sort_tuple(data):
return sorted(data, key=lambda item: float(item[1]), reverse=True)
# Sample data
data = [('Dengue', '54.865'), ('Malaria', '345.743'), ('Corona', '456.864'),
('Typhoid', '35.285'), ('Jaundice', '83.367')]
result = sort_tuple(data)
print("Original data:", data)
print("Sorted result:", result)
Original data: [('Dengue', '54.865'), ('Malaria', '345.743'), ('Corona', '456.864'), ('Typhoid', '35.285'), ('Jaundice', '83.367')]
Sorted result: [('Corona', '456.864'), ('Malaria', '345.743'), ('Jaundice', '83.367'), ('Dengue', '54.865'), ('Typhoid', '35.285')]
Using sort() Method
The sort() method modifies the original list in-place, which is more memory-efficient for large datasets ?
def sort_in_place(data):
data.sort(key=lambda item: float(item[1]), reverse=True)
return data
# Sample data
data = [('638', '54.865'), ('932', '345.743'), ('256', '456.864'),
('843', '35.285'), ('246', '83.367')]
print("Original data:", data)
sort_in_place(data)
print("After sorting:", data)
Original data: [('638', '54.865'), ('932', '345.743'), ('256', '456.864'), ('843', '35.285'), ('246', '83.367')]
After sorting: [('256', '456.864'), ('932', '345.743'), ('246', '83.367'), ('638', '54.865'), ('843', '35.285')]
Using Bubble Sort Algorithm
This approach implements bubble sort manually to demonstrate how sorting works at a lower level ?
# Sample data with actual float values
data = [('638', 54.865), ('932', 345.743), ('256', 456.864),
('843', 35.285), ('246', 83.367)]
print("Original data:", data)
# Bubble sort implementation
length = len(data)
for i in range(length):
for j in range(length - i - 1):
if data[j][1] < data[j + 1][1]: # Compare float values
# Swap elements
data[j], data[j + 1] = data[j + 1], data[j]
print("Sorted data:", data)
Original data: [('638', 54.865), ('932', 345.743), ('256', 456.864), ('843', 35.285), ('246', 83.367)]
Sorted data: [('256', 456.864), ('932', 345.743), ('246', 83.367), ('638', 54.865), ('843', 35.285)]
Comparison
| Method | Modifies Original | Time Complexity | Best For |
|---|---|---|---|
sorted() |
No | O(n log n) | Preserving original data |
sort() |
Yes | O(n log n) | Memory efficiency |
| Bubble Sort | Yes | O(n²) | Learning purposes |
Conclusion
Use sorted() when you need to preserve the original data, and sort() for in-place sorting. Both built-in methods are more efficient than manual implementations for production code.
