
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to sort a list of tuples alphabetically
When it is required to sort a list of tuples in alphabetical order, the 'sort' method can be used. When using this, the contents of the original tuple get changed, since in-place sorting is performed.
The 'sort' function sorts the values in ascending order by default. If the order of sorting is specified as descending, it is sorted in descending order.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list.
Below is a demonstration of the same −
Example
def sort_tuple_vals(my_tup): my_tup.sort(key = lambda x: x[0]) return my_tup my_tup = [("Hey", 18), ("Jane", 33), ("Will", 56),("Nysa", 35), ("May", "Pink")] print("The tuple is ") print(my_tup) print("After sorting the tuple alphabetically, it becomes : ") print(sort_tuple_vals(my_tup))
Output
The tuple is [('Hey', 18), ('Jane', 33), ('Will', 56), ('Nysa', 35), ('May', 'Pink')] After sorting the tuple alphabetically, it becomes : [('Hey', 18), ('Jane', 33), ('May', 'Pink'), ('Nysa', 35), ('Will', 56)]
Explanation
- A function named 'sort_tuple_vals' is defined, that takes a tuple as a parameter.
- It uses the sort method and the lambda function to sort the elements in the tuple.
- This is returned when the function is called.
- Now the tuple is defined, and the function is called by passing this tuple as the parameter.
- The output is displayed on the console.
- Related Articles
- Python program to sort a list of tuples by second Item
- Sort list of tuples by specific ordering in Python
- Python program to find Tuples with positive elements in a List of tuples
- Python program to convert a list of tuples into Dictionary
- Python program to find Tuples with positive elements in List of tuples
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Python program to Sort Tuples by their Maximum element
- Python program to sort tuples by frequency of their absolute difference
- Python program to convert elements in a list of Tuples to Float
- How to sort the letters in a string alphabetically in Python?
- Python program to Convert a elements in a list of Tuples to Float
- Python program to Order Tuples using external List
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Convert list of tuples to list of list in Python

Advertisements