Tuples are immutable objects. However, sometimes, we might need to sort a tuple. In this article, we will discuss different ways to sort a tuple in Python.
Sort a Tuple Using The sort() Method
Tuples in Python are immutable and we cannot sort a tuple directly. However, we can use a list to create a sorted tuple from a given tuple. For this, we will use the following steps.
- First, we will convert the tuple into a list using the
list()
function. Thelist()
function takes the tuple as its input argument and returns a list with the same elements as the tuple. - Once we get the list, we will sort it using the
sort()
method. Thesort()
method, when invoked on a list, sorts the list in ascending order. - After sorting the list, we will convert it into a tuple. For this, we will use the
tuple()
function. Thetuple()
function takes the sorted list as its input argument and returns a sorted tuple.
Using the above steps, we can sort a tuple in Python. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
myList=list(myTuple)
myList.sort()
output=tuple(myList)
print("The original tuple is:")
print(myTuple)
print("The sorted tuple is:")
print(output)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The sorted tuple is:
(2, 4, 11, 12, 15, 21, 23, 44)
To sort the tuple in descending order, you can set the reverse
parameter to True in the sort()
method. After this, the sort()
method sorts the list in descending order. Hence, we get the output tuple sorted in descending order. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
myList=list(myTuple)
myList.sort(reverse=True)
output=tuple(myList)
print("The original tuple is:")
print(myTuple)
print("The sorted tuple is:")
print(output)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The sorted tuple is:
(44, 23, 21, 15, 12, 11, 4, 2)
Sort a Tuple Using The sorted() Function
To sort a tuple in Python, we can also use the sorted()
function. The sorted()
function takes the tuple as its input argument and returns a list containing all the elements of the tuple in sorted order. Once we get the list, we can convert it into a tuple using the tuple()
function. The tuple()
function takes the sorted list as its input argument and returns the sorted tuple. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
sorted_list=sorted(myTuple)
output=tuple(sorted_list)
print("The original tuple is:")
print(myTuple)
print("The sorted tuple is:")
print(output)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The sorted tuple is:
(2, 4, 11, 12, 15, 21, 23, 44)
In the above example, the output tuple contains the elements in ascending order. You can also sort the tuple in descending order. For this, you can set the reverse
parameter to True in the sorted()
function as shown below.
myTuple=(11,2,44,21,15,4,23,12)
sorted_list=sorted(myTuple, reverse=True)
output=tuple(sorted_list)
print("The original tuple is:")
print(myTuple)
print("The sorted tuple is:")
print(output)
Output:
The original tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The sorted tuple is:
(44, 23, 21, 15, 12, 11, 4, 2)
Conclusion
In this article, we discussed ways to sort a tuple in Python. To learn more about Python programming, you can read this article on how to find the max value of a list in Python. You might also like this article on the pandas where method.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.