check if tuple is sorted in python

Check If a Tuple is Sorted in Python

A tuple is an ordered collection of items in Python. That is, there’s an order to the elements present inside a tuple. In this tutorial, we will look at how to check if a tuple is sorted or not in Python with the help of some examples.

check if tuple is sorted in python

You can use the following methods to check if a tuple is sorted or not –

  1. Iterate through the tuple and see if the elements are in order.
  2. Compare the tuple with the corresponding sorted tuple.

Let’s now look at the above methods through some examples.

Iterate through the tuple in Python to check if it’s sorted or not. If any element is out of order, we can say that the tuple is not sorted.

For example, to check if a tuple is sorted in ascending order, we check if the current element is smaller than the previous element. If for any element this condition is satisfied, we can say that the tuple is not sorted.

Let’s look at an example.

# function to check if tuple is sorted in ascending order
def is_tuple_sorted(t):
    for i in range(1, len(t)):
        # return False if the element is smaller than the previous element
        if t[i] < t[i-1]:
            return False
    return True

# create a tuple
t = (1, 2, 3, 4, 5)
# check if tuple is sorted
print(is_tuple_sorted(t))

Output:

True

We get True as the output because all the elements in the tuple are present in ascending order.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

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

You can similarly check if elements are present in descending order or not by modifying the condition.

# function to check if tuple is sorted in descending order
def is_tuple_sorted(t):
    for i in range(1, len(t)):
        # return False if the element is greater than the previous element
        if t[i] > t[i-1]:
            return False
    return True

# create a tuple
t = (5, 4, 3, 2, 1)
# check if tuple is sorted
print(is_tuple_sorted(t))

Output:

True

We get True as the output.

The worst-case time complexity of this method is O(N).

Here, we use a combination of the Python built-in sorted() and tuple() function to get a sorted copy of the original tuple. We compare this sorted tuple to the original tuple to check if it’s sorted or not.

Let’s look at an example.

# create a tuple
t = (1, 2, 3, 4, 5)
# check if tuple is sorted
print(t == tuple(sorted(t)))

Output:

True

We get the same result as above.

Let’s look at another example.

# create a tuple
t = (1, 4, 3, 5, 2)
# check if tuple is sorted
print(t == tuple(sorted(t)))

Output:

False

Here we get False as the output because the tuple is not sorted.

The worst-case time complexity of this method is O(NlogN).

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top