GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Python Sort() Method
  • Sort Python Dictionaries By Key or Value

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python sorted() Function - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python sorted() Function

Last Updated : 20 Dec, 2025

sorted() function in Python returns a new sorted list from the elements of any iterable, such as a list, tuple, set, or string. It does not modify the original iterable, unlike the sort() method for lists.

Let's start with a basic example of sorting a list of numbers in ascending order using the sorted() function.

Python
a = [4, 1, 3, 2]
b = sorted(a)
print(b)

Output
[1, 2, 3, 4]

Syntax

sorted(iterable, key=None, reverse=False)

Parameters:

  • iterable: The sequence to be sorted (list, tuple, set, string, etc.)
  • key (Optional): A function to customize the sort order. Default is None.
  • reverse (Optional): If True, sorts in descending order. Default is False.

Return Type: Returns a new sorted list containing all elements from the iterable according to the given criteria.

Sorting in Descending Order

To sort an iterable in descending order, set the reverse argument to True.

Python
a = [5, 2, 9, 1, 5, 6]
res = sorted(a, reverse=True)
print(res)

Output
[9, 6, 5, 5, 2, 1]

Sorting a String

You can sort the characters of a string and return a list.

Python
s = "python"
res = sorted(s)
print(res)  

Output
['h', 'n', 'o', 'p', 't', 'y']

Explanation: Each character of the string is treated as an element and sorted in ascending (alphabetical) order.

Sorting a Tuple

You can sort the elements of a tuple using sorted(), which returns a list.

Python
t = (3, 1, 4, 2)
res = sorted(t)
print(res) 

Output
[1, 2, 3, 4]

Explanation: Tuples are immutable, so sorted() returns a new sorted list, leaving the original tuple unchanged.

Sorting using key Parameter

The key parameter is an optional argument that allows us to customize the sort order.

1. Sorting Strings by Length: You can sort a list of strings by their length using the key=len parameter in sorted().

Python
a = ["apple", "banana", "cherry", "date"]
res = sorted(a, key=len)
print(res)

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

Explanation: The key parameter is set to len, which sorts the words by their length in ascending order.

2. Sorting a List of Dictionaries: You can sort a list of dictionaries based on a specific key using sorted() with a lambda function.

Python
a = [
    {"name": "Harry", "score": 85},
    {"name": "Leo", "score": 91},
    {"name": "Eve", "score": 78}
]

b = sorted(a, key=lambda x: x['score'])
print(b)

Output
[{'name': 'Eve', 'score': 78}, {'name': 'Harry', 'score': 85}, {'name': 'Leo', 'score': 91}]

Explanation: key=lambda x: x['score'] specifies that the sorting should be done using the 'score' value from each dictionary

Related Articles:

  • Python Sort() Method
  • Sort Python Dictionaries By Key or Value
  • How to Sort a Set of Values?
  • How to Sort a Dictionary by Value?
  • Sorting Objects of User Defined Class
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.