list.clear()

Python · Lists

Theory

.clear() removes every item from a list, leaving it empty, and returns None. sales = [100, 250, 300] then sales.clear() leaves sales as []. The important detail is that it empties the same list object rather than creating a new one, so every variable that refers to that list sees it become empty. The items themselves are only freed from memory if nothing else refers to them.

Compare it with sales = []. Assignment creates a brand-new empty list and points only the name sales at it; any other name still refers to the old list with all its items. Which one you want depends on whether other code shares the list. clear() is equivalent to del sales[:]. Dictionaries and sets have a .clear() method that works the same way.

Why we use it

When a list is reused as a buffer or shared between parts of a program, you sometimes need to empty it for everyone at once, for example after writing a batch of rows to a file. clear() does that without breaking the shared reference.

When to use it

Use clear() when other names or objects hold the same list and should all see it empty. Use reassignment (sales = []) when you just want a fresh list for this one name and do not care about other references.

How it works

  1. Call sales.clear()
  2. Release the references to every item
  3. Set the list's length to 0
  4. The list object itself stays the same
  5. Does another name refer to this list? Yes: That name now sees [] as well. No: Nothing else is affected.
  6. print(sales) shows []

Syntax

list.clear()

Example

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