Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Check if list is Matrix
Next article icon

Check if a List is Sorted or not – Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a list of numbers and our task is to check whether the list is sorted in increasing or decreasing order. For example, if the input is [1, 2, 3, 4], the output should be True, but for [3, 1, 2], it should be False.

Using all()

all() function checks if every pair of consecutive elements in the list is in non-decreasing order.

Python
a = [1, 2, 3, 4, 5]  # For checking ascending order print(all(a[i] <= a[i + 1] for i in range(len(a) - 1)))  b = [5, 4, 3, 2, 1]  # For checking descending order print(all(b[i] >= b[i + 1] for i in range(len(b) - 1))) 

Output
True True 

Explanation:

  • For the list a = [1, 2, 3, 4, 5], we are checking for ascending order and here all() method is used verify if each element is less than or equals to its next element in the list. It only returns True if the list is sorted in ascending order.
  • For the list b = [5, 4, 3, 2, 1], we are checking descending order all() method verify if each element is greater than or equals to its next element in the list. It returns True if the list is sorted in descending order.

Note: To check for strictly increasing or decreasing sequences remove the equals sign (=) from the comparison.

Using sorted()

sorted() function returns a sorted version of the list. If the original list is equal to its sorted version it means the list is already sorted.

Python
a = [1, 2, 3, 4, 5] b = [5, 4, 3, 2, 1]  # Here, sorted(), sort list in ascending order print(a == sorted(a))   print(b == sorted(b))  

Output
True False 

Explanation:

  • sorted(a) returns a new sorted list.
  • a == sorted(a) compares the original list with its sorted version. If they match, the list is sorted.

Note: We can also use list.sort() method as well. To do this, we’ll create a copy of the original list, sort the copy and then compare the two lists.

Using for Loop

We can iterate through the list and check if each element is less than or equal to the next one, if the loop executes completely that means the list is sorted but if we break out of the loop that means the list is not sorted.

Python
a = [1, 2, 3, 4] res = True  for i in range(len(a) - 1):     if a[i] > a[i + 1]:         res = False         break  print(res) 

Output
True 

Explanation:

  • Iterates through the list and checks if each element is greater than the next one.
  • If any element is greater than the next, res is set to False and we break the loop.

Related Articles:

  • Check for Descending Sorted List
  • Sort List of Lists Ascending and then Descending
  • Difference between sorted() and sort()


Next Article
Python | Check if list is Matrix
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python list-programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Check if a list is empty or not in Python
    In article we will explore the different ways to check if a list is empty with simple examples. The simplest way to check if a list is empty is by using Python's not operator. Using not operatorThe not operator is the simplest way to see if a list is empty. It returns True if the list is empty and F
    2 min read
  • Python | Check if list is Matrix
    Sometimes, while working with Python lists, we can have a problem in which we need to check for a Matrix. This type of problem can have a possible application in Data Science domain due to extensive usage of Matrix. Let's discuss a technique and shorthand which can be used to perform this task.Metho
    3 min read
  • Check for True or False in Python
    Python has built-in data types True and False. These boolean values are used to represent truth and false in logical operations, conditional statements, and expressions. In this article, we will see how we can check the value of an expression in Python. Common Ways to Check for True or FalsePython p
    2 min read
  • How to check if an object is iterable in Python?
    In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case). In this article, we are going to see how to check
    3 min read
  • Check if two lists are identical in Python
    Our task is to check if two lists are identical or not. By "identical", we mean that the lists contain the same elements in the same order. The simplest way to check if two lists are identical using the equality operator (==). Using Equality Operator (==)The easiest way to check if two lists are ide
    2 min read
  • Python - Sort list of list by specified index
    When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
    3 min read
  • Python sorted containers | An Introduction
    Sorted Containers is a powerful Python library that provides fast and easy-to-use implementations of SortedList, SortedDict and SortedSet data types. Unlike Python’s built-in types, these containers maintain their elements in sorted order automatically as elements are added or removed. To use this l
    3 min read
  • Python | sort list of tuple based on sum
    Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple. Examples: Input: [(4, 5), (2, 3), (6, 7), (2, 8)] Output: [(2, 3), (4, 5), (2, 8), (6, 7)] Input: [(3, 4), (7, 8), (6, 5)] Output: [(3, 4), (6, 5), (7, 8)] # Method 1: Using bubble sort Using th
    4 min read
  • Python - Returning index of a sorted list
    We are given a list we need to return the index of a element in a sorted list. For example, we are having a list li = [1, 2, 4, 5, 6] we need to find the index of element 4 so that it should return the index which is 2 in this case. Using bisect_left from bisect modulebisect_left() function from bis
    3 min read
  • Creating a Sorted Merged List of Two Unsorted Lists in Python
    Creating a sorted merged list of two unsorted lists involves combining the elements of both lists and sorting the resulting list in ascending order. For example: If we have list1 = [25, 18, 9] and list2 = [45, 3, 32] the output will be [3, 9, 18, 25, 32, 45]. Using + OperatorThis method merges the t
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences