Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

set

set creates a mutable, unordered collection of distinct hashable objects. It supports membership, iteration, deduplication, and set operations.

How it works

The set class builds a mutable collection of distinct hashable elements from an optional iterable. Sets support membership tests, iteration, length checks, and mathematical operations, but they do not preserve positional order or support indexing.

Syntax

class set(iterable=(), /)

Parameters

NameTypeDescription
iterableiterableOptional iterable whose elements become the new set's members; omitting it creates an empty set.

Returns

A new mutable set containing the distinct hashable elements taken from iterable; without an iterable, an empty set.

Key points

  • A set contains unique values, and each value must be hashable.
  • Use sets to test membership, remove duplicates, and perform set algebra.
  • Sets support membership checks, cardinality queries, and iteration.
  • Modify a set with mutating methods such as add() and remove().
  • The constructor copies elements from an iterable into a new set.
  • Use brace literals for non-empty sets, for example {1, 2, 3}.

Common mistakes

  • Do not rely on insertion order, and do not use indexing or slicing with a set.
  • A mutable set cannot be a dictionary key or a member of another set; use frozenset when a hashable set-like value is needed.
  • Every element must be hashable, so mutable containers such as lists cannot be direct set members.

Example: Create and use a set

tags = {"python", "backend", "python"}
tags.add("scripting")

print("python" in tags)
print(sorted(tags))
print(tags & {"python", "web"})

Creates a set that removes a duplicate, adds an element, tests membership, and computes an intersection.

When to use it

Membership testing

Checking whether a value belongs to a collection efficiently and directly.

Removing duplicates

Converting an iterable into unique values.

Set algebra

Comparing or combining collections with union, intersection, difference, or symmetric difference.

Interview questions

QWhat does the set constructor accept, and what does it return?

set(iterable) returns a new mutable set containing the iterable's distinct hashable elements. Calling set() without an iterable creates an empty set.

Follow-up: How would you represent a set as a dictionary key? Use frozenset, whose contents cannot be changed and which is hashable.

QWhat are common uses and supported operations of sets?

A set stores distinct elements without positional order. It supports membership testing, length, iteration, and operations such as union, intersection, difference, and symmetric difference.

Follow-up: Why is a set useful for deduplication? Constructing it from an iterable retains one occurrence of each distinct hashable value.

QWhy can’t sets be indexed like lists?

The set type is mutable, so methods such as add() and remove() can change its contents. Because it is unordered, it does not support indexing or slicing.

Follow-up: What should code use instead of positional access? Iterate over the set or use a separate ordered collection when order matters.

  • frozenset
  • set comprehension
  • add()
  • remove()
  • union
  • intersection
  • difference
  • symmetric difference

Source: Built-in Types. Checked 2026-10-15.