Example:
tuple = ("python", "includehelp", 43, 54.23)
Sorting List of Tuples Alphabetically
We need to sort all the tuples of the list alphabetically using the first elements of each tuple. To solve will be simply sorting the list taking the first elements of the tuples as a sorting index for the list.
Input:
[("python", 3), ("scala", 7), ("C", 1), ("java", 5)]
output:
[("C", 1), ("java", 5), ("python", 3), ("scala", 7)]
In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.
Method 1:
One method to solve the problem is by using the bubble sort technique. In this technique, we will be accessing the first element of each tuple and sort using a technique similar to bubble sort.
Output:
Method 2:
Another method to solve the problem is by using python's built-in sort() method. The method performs in-place sorting of values in Python.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer