Python program to sort a list of tuples alphabetically

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).



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output:

[('Abhishek', 29), ('Amana', 28), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.