list.sort()

Python · Lists

Theory

.sort() rearranges the items of a list in place into ascending order and returns None. sales = [300, 100, 250] then sales.sort(reverse=True) gives [300, 250, 100]. Numbers sort by value and strings sort by character code, so uppercase letters come before lowercase ones. A list mixing types that cannot be compared, like [3, 'a'], raises TypeError.

The key argument takes a function that is called once per item, and items are ordered by its result: names.sort(key=len) sorts by length and names.sort(key=str.lower) sorts case-insensitively. Python's sort (Timsort) runs in O(n log n) and is stable, meaning items with equal keys keep their original relative order, which lets you sort by several fields in steps. If you need to keep the original list, use sorted(sales), which returns a new sorted list and works on any iterable.

Why we use it

Ranked output is everywhere: top customers by revenue, latest dates first, names alphabetically. sort() orders data efficiently, and key= lets you sort by any rule without rewriting the data.

When to use it

Use list.sort() when you no longer need the original order and want to save memory. Use sorted() to keep the original, or to sort a tuple, dict keys or other iterables. Never write x = x.sort(); that stores None.

How it works

  1. Call sales.sort(reverse=True)
  2. Was a key function given? Yes: Compute key(item) for each item. No: Compare the items themselves.
  3. Order items by those values (descending because reverse=True)
  4. Rearrange the same list object in place
  5. Return None
  6. print(sales) shows [300, 250, 100]

Syntax

list.sort()
list.sort(reverse=True)

Example

sales = [300, 100, 250]
sales.sort(reverse=True)
print(sales)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic