Python program to sort a list of tuples alphabetically
Last Updated :
25 Apr, 2023
Given a list of tuples, write a Python program to sort the tuples alphabetically by the first item of each tuple. Examples:
Input: [("Amana", 28), ("Zenat", 30), ("Abhishek", 29), ("Nikhil", 21), ("B", "C")]
Output: [('Amana', 28), ('Abhishek', 29), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
Input: [("aaaa", 28), ("aa", 30), ("bab", 29), ("bb", 21), ("csa", "C")]
Output: [('aa', 30), ('aaaa', 28), ('bab', 29), ('bb', 21), ('csa', 'C')]
Method 1: Using Bubble sort Using the technique of Bubble Sort to we can perform the sorting. Note that each tuple is an element in the given list. Access the first element of each tuple using the nested loops. This performs the in-place method of sorting. The time complexity is similar to the Bubble Sort i.e. O(n^2).
Python3
# Python program to sort a
# list of tuples alphabetically
# Function to sort the list of
# tuples
def SortTuple(tup):
# Getting the length of list
# of tuples
n = len(tup)
for i in range(n):
for j in range(n-i-1):
if tup[j][0] > tup[j + 1][0]:
tup[j], tup[j + 1] = tup[j + 1], tup[j]
return tup
# Driver's code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
print(SortTuple(tup))
Output:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
Method 2: Using sort() method While sorting via this method the actual content of the tuple is changed, and just like the previous method, the in-place method of the sort is performed.
Python3
# Python program to sort a list of
# tuples using sort()
# Function to sort the list
def SortTuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using first element of
# sublist lambda has been used
tup.sort(key = lambda x: x[0])
return tup
# Driver's code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
print(SortTuple(tup))
Output:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
Method 3: Using sorted() method sorted() method is a method of list class that returns the sorted list without making any changes to the original list.
Python3
# Python program to sort a list of
# tuples using sorted()
# Function to sort the list
def Sort_Tuple(tup):
# reverse = None (Sorts in Ascending order)
# key is set to sort using first element of
# sublist lambda has been used
return(sorted(tup, key = lambda x: x[0]))
# Driver Code
tup = [("Amana", 28), ("Zenat", 30), ("Abhishek", 29),
("Nikhil", 21), ("B", "C")]
# printing the sorted list of tuples
print(Sort_Tuple(tup))
Output:
[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]
Time Complexity: O(n*logn), as sorted() function is used.
Auxiliary Space: O(n), where n is length of list.
Similar Reads
Python Program to Sort Words in Alphabetical Order The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() m
2 min read
How to Sort a List Alphabetically in Python? Sorting lists alphabetically is a common task in Python, whether we're organizing names, cleaning data or building an interface. Python makes sorting easy with several built-in methods and libraries for both simple and advanced sorting needs.Let's look at some of the most common methods one by one:U
2 min read
Python | Ways to sort letters of string alphabetically Given a string of letters, write a python program to sort the given string in an alphabetical order. Example: Input : PYTHON Output : HNOPTY Input : Geeks Output : eeGksNaive Method to sort letters of string alphabetically Here we are converting the string into list and then finally sorting the enti
2 min read
Python program to Sort Tuples by their Maximum element Given a Tuple List sort tuples by maximum element in a tuple. Input : test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)] Explanation : 19 > 7 = 7 > 2, is order, hence reverse sorted by maximum element. Input : test_list
5 min read
Sort list of tuples by specific ordering - Python Sorting a list of tuples by a specific order requires us to define a custom logic for the sorting process. For example, given a list of tuples like [(1, 'Python'), (2, 'with'), (3, 'GFG')], we might want to sort it based on a predefined order of the second element such as ['with', 'GFG', 'Python'].
3 min read