• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Sort a Tuple in Python

Sort a Tuple in Python

Author: Aditya Raj
Last Updated: April 23, 2023

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.

Table of Contents
  1. Sort a Tuple Using The sort() Method
  2. Sort a Tuple Using The sorted() Function
  3. Conclusion

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. The list() 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. The sort() 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. The tuple() 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!

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us