- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
In Python, you can sort a list using the sorted() function or the sort() method. The sorted() function returns a new sorted list, while the sort() method sorts the list in place.
Example: Using sorted()
numbers = [6, 9, 3, 1]sorted_numbers = sorted(numbers)print(sorted_numbers) # Output: [1, 3, 6, 9]print(numbers) # Output: [6, 9, 3, 1]Copied!✕CopyExample: Using sort()
numbers = [6, 9, 3, 1]numbers.sort()print(numbers) # Output: [1, 3, 6, 9]Copied!✕CopyKey Differences:
sorted(): Returns a new sorted list and does not modify the original list.
sort(): Modifies the original list in place and returns None.
Sorting with Custom Key:
You can use the key parameter to specify a function to be called on each list element prior to making comparisons.
words = ["banana", "pie", "Washington", "book"]sorted_words = sorted(words, key=len)print(sorted_words) # Output: ['pie', 'book', 'banana', 'Washington']Copied!✕CopySorting in Reverse Order:
Both sorted() and sort() accept a reverse parameter to sort in descending order.
Sorting Techniques — Python 3.14.3 documentation
1 day ago · In this document, we explore the various techniques for sorting data using Python. A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list: You can also …
See results only from docs.python.orgUnicode Howto
The String Type ¶ Since Python 3.0, the language’s str type contains Unicode …
Timecomplexity
As seen in the source code the complexities for set difference s-t or s.difference (t) …
Found a Bug
Dealing with Bugs ¶ Python is a mature programming language which has …
PEP 8
This document gives coding conventions for the Python code comprising the …
Built-In Functions
Built-in Functions ¶ The Python interpreter has a number of functions and types built …
Python 3.10.17 Documentati…
Sorting HOW TO ¶ Author Andrew Dalke and Raymond Hettinger Release 0.1 Python …
Modules
Python Module Index _ | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | …
History and License
Terms and conditions for accessing or otherwise using Python ¶ Python …
Table of Contents
What’s New in Python- What’s new in Python 3.14- Summary – Release …
Data Structures
5. Data Structures ¶ This chapter describes some things you’ve learned about already …
Sorting Algorithms in Python - GeeksforGeeks
Dec 12, 2025 · It was Created by Tim Peters in 2002, Tim Sort is the default sorting algorithm in Python and is renowned for its speed and efficiency in real-world …
Python List sort () Method - W3Schools
Definition and Usage The sort() method sorts the list ascending by default. You can also make a function to decide the sorting criteria (s).
Code sample
# Sort the list alphabeticallycars = ['Ford', 'BMW', 'Volvo']cars.sort()How to Use sorted() and .sort() in Python – Real Python
May 6, 2019 · In this tutorial, you’ll learn how to sort various types of data in different data structures, customize the order, and work with two different ways …
How To Sort An Array In Python?
Dec 23, 2024 · Learn how to sort arrays in Python using built-in methods like sort () and sorted (), and explore custom sorting with lambda functions in this easy-to-follow guide
Learn Python Programming - Python Development Certificate
Sponsored Prepare for a new career by building in-demand Python skills with Microsoft training. Design, develop, deploy. Earn the Microsoft Python Development Professional Certificate.