ADVERTISEMENT
ADVERTISEMENT

Python program to sort list of tuples alphabetically

In this program, we are given a list of tuples with each tuple consisting of string elements as the first element of the tuple. We need to create a Python program to sort the list of tuples alphabetically by the first elements of the tuple.
Submitted by Shivang Yadav, on November 26, 2021

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

# Initializing and printing list of tuples 
tupleList = [("python", 3), ("scala", 7), ("C", 1), ("java", 5)]
print("The elements of list of tuples are : " + str(tupleList))  

# Sorting list of tuples alphabetically
n = len(tupleList)
for i in range(n):
    for j in range(n-i-1):
        if tupleList[j][0] > tupleList[j + 1][0]:
            tupleList[j], tupleList[j + 1] = tupleList[j + 1], tupleList[j]

# Printing sorted list 
print("The elements of sorted list of tuples are : " + str(tupleList))

Output:

The elements of list of tuples are : [('python', 3), ('scala', 7), ('C', 1), ('java', 5)]
The elements of sorted list of tuples are : [('C', 1), ('java', 5), ('python', 3), ('scala', 7)]

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.

# Program to sort list of tuples alphabetically using bubble sort, 
 
# Initializing and printing list of tuples 
tupleList = [("python", 3), ("scala", 7), ("C", 1), ("java", 5)]
print("The elements of list of tuples are : " + str(tupleList))  

# Sorting list of tuples alphabetically
sortedTupleList = sorted(tupleList, key = lambda x: x[0])

# Printing sorted list 
print("The elements of sorted list of tuples are : " + str(sortedTupleList))

Output:

The elements of list of tuples are : [('python', 3), ('scala', 7), ('C', 1), ('java', 5)]
The elements of sorted list of tuples are : [('C', 1), ('java', 5), ('python', 3), ('scala', 7)]

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.