set.remove() / .discard()

Python · Sets

Theory

Both methods delete one item from a set in place and return None, and both are O(1) on average because the item is found by its hash. The difference is what happens when the item is not there. .remove(item) raises KeyError, which is useful when a missing item means something went wrong. .discard(item) silently does nothing, which is convenient when you just want to make sure the item is gone.

With cities = {'Mumbai', 'Delhi'}, cities.discard('Pune') leaves the set unchanged with no error, while cities.remove('Pune') raises KeyError: 'Pune'. A third option, .pop(), removes and returns an arbitrary item (raising KeyError on an empty set), since sets have no 'last' item. As with lists, never remove items from a set while looping over that same set; Python raises RuntimeError because the set changed size during iteration. Loop over a copy instead: for c in set(cities).

Why we use it

Removing a value, such as an unsubscribed user or a resolved ticket, is routine. Having both methods lets you choose between strict behaviour that catches mistakes and forgiving behaviour that keeps code simple.

When to use it

Use discard() when the item may or may not be present and that is fine. Use remove() when absence would be a bug you want to notice. To remove many items, use difference_update() or the -= operator.

How it works

  1. Call cities.discard('Pune') or cities.remove('Pune')
  2. Compute hash('Pune') and look in that slot
  3. Is 'Pune' in the set? Yes: Delete it and return None. No: Go to the next check.
  4. Was remove() used? Yes: Raise KeyError: 'Pune'. No: discard() returns None silently.
  5. The set no longer contains 'Pune'

Syntax

set.remove(item)
set.discard(item)

Example

cities = {'Mumbai', 'Delhi'}
cities.discard('Pune')  # no error even though absent
print(cities)

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