Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
sort() in Python
Next article icon

sort() in Python

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. Example:

Python
a = [5, 3, 8, 1, 2] a.sort() print(a)  a.sort(reverse=True) print(a) 

Output
[1, 2, 3, 5, 8] [8, 5, 3, 2, 1] 

Explanation:

  • a.sort() sorts a in ascending order.
  • a.sort(reverse=True) sorts a in descending order.

Syntax of sort()

list.sort(reverse=False, key=None, m=None)

Parameters:

  • reverse (optional): If True, sorts in descending order; default is False for ascending order.
  • key (optional): A function for custom sorting conditions.
  • m (optional): A custom parameter for additional sorting behavior, like a threshold.

Returns: This method returns None as it does not create a new list, it simply modifies the original list in place.

Examples of sort()

Example 1: In this example, we arrange a list of strings in alphabetical (lexicographical) order.

Python
a = ["banana", "apple", "cherry"] a.sort() print(a) 

Output
['apple', 'banana', 'cherry'] 

Explanation: a.sort(reverse=True) sorts the list in descending order, from Z to A, based on their Unicode code points.

Example 2: In this example, we sort a list of strings by their length using the key parameter of the sort() method. By passing the built-in len function, the strings are sorted in ascending order based on length.

Python
a = ["sun", "moonlight", "sky"] a.sort(key=len) print(a) 

Output
['sun', 'sky', 'moonlight'] 

Explanation: a.sort(key=len) sorts the list based on the length of each string, arranging them in ascending order of their lengths.

Example 3: In this example, we sort a list of dictionaries by the value of the "age" key using the key parameter of the sort() method.

Python
d = [     {"name": "Alice", "age": 30},     {"name": "Bob", "age": 25},     {"name": "Charlie", "age": 35} ]  d.sort(key=lambda person: person["age"]) print(d) 

Output
[{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}] 

Explanation: d.sort(key=lambda person: person["age"]) sorts the list of dictionaries by the "age" key in ascending order. The lambda function extracts the age from each dictionary for comparison.

Example 4: In this example, we sort a list of tuples by the second element (age) using the key parameter with a lambda function, sorting the list in ascending order by age.

Python
a = [("Alice", 25), ("Bob", 30), ("Charlie", 22)] a.sort(key=lambda x: x[1]) print(a) 

Output
[('Charlie', 22), ('Alice', 25), ('Bob', 30)] 

Explanation: a.sort(key=lambda x: x[1]) sorts the list of tuples by the second element age in ascending order. The lambda function accesses the second element of each tuple for comparison.

Difference between sorted() and sort() function in Python

Understanding the difference between sorted() and sort() helps you choose the right tool for your needs. Both sort elements but differ in memory usage, stability, and compatibility. Let’s break it down in the following table.

Feature

sorted()

sort()

Return Type

Returns a new sorted list

Sorts the list in place

Order

Can specify ascending/descending

Default is ascending

Applicable to

Any iterable (not just lists)

Only lists

Stability

Stable (maintains relative order)

May not be stable

Memory Usage

Requires extra memory

Sorts in place, no extra memory

Key Parameter

Supports key for custom sorting

Supports key for custom sorting

Time Complexity

O(n log n)

O(n log n)

Related Articles

  • Lambda
  • sorted() method
  • Python difference between the sorted() and sort() function.

Next Article
sort() in Python

S

ShivamKD
Improve
Article Tags :
  • Python
  • python-list
  • python-list-functions
Practice Tags :
  • python
  • python-list

Similar Reads

    Insertion Sort - Python
    Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list.Insertion SortThe insertionSort function takes an array arr as input. It first calculates the length of the array (n). If the le
    3 min read
    Python sorted() Function
    sorted() function returns a new sorted list from the elements of any iterable like (e.g., list, tuples, strings ). It creates and returns a new sorted list and leaves the original iterable unchanged. Let's start with a basic example of sorting a list of numbers using the sorted() function.Pythona =
    3 min read
    Shell Sort - Python
    Shell Sort is an advanced version of the insertion sort algorithm that improves its efficiency by comparing and sorting elements that are far apart. The idea behind Shell Sort is to break the original list into smaller sublists, sort these sublists, and gradually reduce the gap between the sublist e
    2 min read
    Radix Sort - Python
    Radix Sort is a linear sorting algorithm that sorts elements by processing them digit by digit. It is an efficient sorting algorithm for integers or strings with fixed-size keys. Rather than comparing elements directly, Radix Sort distributes the elements into buckets based on each digit’s value. By
    3 min read
    Python List sort() Method
    The sort() method in Python is a built-in function that allows us to sort the elements of a list in ascending or descending order and it modifies the list in place which means there is no new list created. This method is useful when working with lists where we need to arranged the elements in a spec
    3 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