list.reverse()

Python · Lists

Theory

.reverse() flips the order of the items of a list in place: the first item becomes the last and vice versa. sales = [100, 250, 300] then sales.reverse() gives [300, 250, 100]. It does not look at the values at all, so it is not the same as sorting in descending order; [3, 1, 2] reversed is [2, 1, 3]. It returns None and runs in O(n), simply swapping items from both ends towards the middle.

There are two other ways to get reversed data without changing the original. The slice sales[::-1] creates a new reversed list (a copy, using extra memory). The built-in reversed(sales) returns a lightweight iterator that walks the list backwards without copying, ideal in a for loop: for s in reversed(sales). Choose based on whether you need to keep the original order and whether you need a real list.

Why we use it

Data often comes oldest-first when you want newest-first, or you need to process items from the end. reverse() flips the order cheaply without building a second list.

When to use it

Use reverse() when you want the list itself flipped. Use sales[::-1] when you need a reversed copy and the original untouched, reversed() when you only loop backwards, and sort(reverse=True) when you actually want descending values.

How it works

  1. Call sales.reverse()
  2. Point at the first and last items
  3. Swap them and move both pointers one step inward
  4. Have the pointers met or crossed? Yes: Stop and return None. No: Swap the next pair.
  5. print(sales) shows [300, 250, 100]

Syntax

list.reverse()

Example

sales = [100, 250, 300]
sales.reverse()
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