list.remove()

Python · Lists

Theory

.remove(value) searches the list from the start and deletes the first item that is equal (==) to value, shifting later items left to close the gap. sales = [100, 250, 300, 250] then sales.remove(250) gives [100, 300, 250]: only the first 250 disappears. If no item matches, it raises ValueError: list.remove(x): x not in list. It returns None.

remove() works by value; to delete by position use pop(index) or del sales[index]. Because it must search and then shift, it is O(n). To remove every occurrence, do not call remove() inside a for loop over the same list, since changing a list while iterating over it skips items. Build a filtered copy instead: sales = [s for s in sales if s != 250]. Guard against the error with if value in sales: before removing.

Why we use it

Lists often contain a value you need to drop: a cancelled order, a placeholder, a bad reading. remove() deletes it by what it is, without you having to find its position first.

When to use it

Use remove() to delete one known value. Use pop() or del when you know the index, a list comprehension to remove all matches, and a set if you frequently delete items by value from a large collection.

How it works

  1. Call sales.remove(250)
  2. Scan items from index 0
  3. Is this item == 250? Yes: Delete it, shift later items left, and stop. No: Move to the next item.
  4. Reached the end with no match? Yes: Raise ValueError. No: (already removed).
  5. print(sales) shows [100, 300, 250]

Syntax

list.remove(value)

Example

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