Tuples, like lists, are an ordered collection of items in Python. That is, there is an order to which the elements are present inside a tuple. There might be use-cases where you’d want to sort a tuple in a particular order (for example, ascending or descending). In this tutorial, we will look at how to sort elements of a tuple in Python with the help of some examples.
How to sort a tuple in Python?
Tuples are immutable in Python and thus cannot be altered after creation. You can, however, use the Python built-in sorted()
function to create a sorted copy of the original tuple. The following is the syntax.
# create a tuple with sorted elements from tuple a tuple(sorted(a))
The sorted()
function returns a list of elements in a sorted order which can further be converted to a tuple using the tuple()
function.
Let’s look at some examples.
Sort tuple in ascedning order
The sorted()
function, by default, sorts an iterable in ascending order and returns the sorted elements as a list. Let’s use it to sort a tuple in ascending order.
# creat a tuple of numbers a = (2, 4, 3, 5, 1) # sort the tuple elements and store the result in variable a a = tuple(sorted(a)) print(a)
Output:
(1, 2, 3, 4, 5)
Here we reassign the returned tuple to the original variable a
which now contains elements sorted in ascending order.
Sort tuple in descending order
You can similarly get a tuple with elements sorted in descending order by passing reverse=True
to the sorted()
function. The reverse
parameter is False
by default.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# creat a tuple of numbers a = (2, 4, 3, 5, 1) # sort the tuple elements in descending sorder and store the result in variable a a = tuple(sorted(a, reverse=True)) print(a)
Output:
(5, 4, 3, 2, 1)
The tuple a
now contains elements sorted in descending order.
Keep in mind that tuples are immutable. Here the sorted()
function returns the sorted elements in a list which we are converting to a tuple using the tuple()
function.
You might also be interested in –
- Python List Sort – With Examples
- Python – Check If List Is Sorted Or Not
- Sort List of Strings Alphabetically in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.