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
def SortTuple(tup):
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
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
def SortTuple(tup):
tup.sort(key = lambda x: x[ 0 ])
return tup
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
def Sort_Tuple(tup):
return ( sorted (tup, key = lambda x: x[ 0 ]))
tup = [("Amana", 28 ), ("Zenat", 30 ), ("Abhishek", 29 ),
("Nikhil", 21 ), ("B", "C")]
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.
Please Login to comment...