Python Sets

Python · Sets

Theory

A set is an unordered collection of unique items, written with curly braces: {'Mumbai', 'Delhi', 'Mumbai'} becomes {'Mumbai', 'Delhi'} because duplicates are dropped automatically. Internally a set is a hash table: Python computes hash(item) to decide where to store each item, so checking x in my_set or adding an item takes roughly constant time on average (O(1)), no matter how big the set is.

That design has consequences. Items must be hashable (immutable), so numbers, strings and tuples are fine, but lists and dicts raise TypeError. There is no index, so my_set[0] is an error, and the printed order is not guaranteed; string sets can even print in a different order between runs. {} creates an empty dict, not a set, so write set() for an empty set. Sets support maths-style operations: union |, intersection &, difference - and symmetric difference ^. frozenset is the immutable version.

Why we use it

Two very common questions are 'what are the distinct values?' and 'is this value in the collection?'. A set answers both efficiently: set(list) deduplicates in one call, and membership tests stay fast even with millions of items.

When to use it

Use a set to remove duplicates, for fast membership checks and to compare groups (common customers, new IDs). Use a list when order or duplicates matter, and dict.fromkeys(items) when you need unique items in their original order.

How it works

  1. Create a set from items, e.g. set(visits)
  2. Take the next item and compute hash(item)
  3. Is an equal item already in the set? Yes: Skip it (no duplicate). No: Store it in the hash table.
  4. Repeat until all items are processed
  5. Result contains each value once; order is not guaranteed

Syntax

my_set = {item1, item2}

Example

unique_cities = {'Mumbai', 'Delhi', 'Mumbai'}
# {'Mumbai', 'Delhi'}

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