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.
How to check if a tuple is sorted or not?
You can use the following methods to check if a tuple is sorted or not –
- Iterate through the tuple and see if the elements are in order.
- Compare the tuple with the corresponding sorted tuple.
Let’s now look at the above methods through some examples.
Iterate through the tuple
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.
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.
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).
Compare tuple with sorted tuple
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.