set.add()

Python · Sets

Theory

.add(item) puts one item into a set, in place, and returns None. If an equal item is already there, nothing happens: no error and no duplicate, which is exactly what makes sets useful for collecting unique values. With cities = {'Mumbai', 'Delhi'}, cities.add('Pune') leaves three cities, and calling cities.add('Pune') again changes nothing.

Under the hood add() hashes the item to find its slot, so it runs in O(1) on average. The item must be hashable: adding a tuple works, but adding a list raises TypeError: unhashable type: 'list'. add() takes exactly one item; to add several at once use update(), which accepts any iterable. Be careful with strings: s.add('Pune') adds one string, while s.update('Pune') adds the letters 'P', 'u', 'n', 'e' separately. Because sets are unordered, the printed position of a new item can vary.

Why we use it

When collecting distinct values in a loop, such as unique customers seen today, add() lets you insert every value without first checking whether it is already there.

When to use it

Use add() for one item at a time inside loops. Use update() to add many items from a list or another set. If you need to keep insertion order or count repeats, use a list, a dict or collections.Counter instead.

How it works

  1. Call ids.add(104)
  2. Compute hash(104) to find its slot
  3. Is an equal item already stored? Yes: Do nothing (no duplicate, no error). No: Store the item.
  4. Return None
  5. The set now holds every distinct value once

Syntax

set.add(item)

Example

cities = {'Mumbai', 'Delhi'}
cities.add('Pune')
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