GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Sets - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Sets

Last Updated : 21 Oct, 2026

Set is a built-in Python data type used to store a collection of unique items.

  • Stores only unique elements; duplicate values are automatically removed.
  • Unordered collection, so elements do not have a fixed position and cannot be accessed using indexes.
  • Supports fast search, insertion and deletion operations using hashing internally.

Example: This example shows how to create a set with some numbers and check its type.

Python
s = {10, 50, 20}
print(s)
print(type(s))

Output
{10, 50, 20}
<class 'set'>

Note: There is no specific order for set elements to be printed

Type Casting

set() method is used to convert other data types, such as lists or tuples, into sets.

Python
s = set(["a", "b", "c"])
print(s)

Output
{'b', 'a', 'c'}

Check unique and Immutable

Sets cannot have duplicate values. While you cannot modify the individual elements directly, you can still add or remove elements from the set.

Python
# a set cannot have duplicate values
s = {"Geeks", "for", "Geeks"}
print(s)

# values of a set cannot be changed
s[1] = "Hello"
print(s)

Output

{'Geeks', 'for'}
TypeError: 'set' object does not support item assignment

Explanation:

  • s = {"Geeks", "for", "Geeks"} duplicates are removed, only unique values remain.
  • s[1] = "Hello" Error, set elements cannot be changed directly. Only add() or remove() can modify a set.

Heterogeneous Element

Sets can store heterogeneous elements in it, i.e., a set can store a mixture of string, integer, boolean, etc datatypes.

Python
s = {"Geeks", "for", 10, 52.7, True}
print(s)

Output
{True, 52.7, 'Geeks', 10, 'for'}

Frozen Sets

Frozenset is an immutable version of a set. Its elements cannot be changed after creation, but you can perform operations like union, intersection and difference. Use frozenset() to create one.

Python
s = set(["a", "b", "c"])
print("Normal Set:", s)

fs = frozenset(["e", "f", "g"])
print("Frozen Set:", fs)

Output
('Normal Set:', set(['a', 'c', 'b']))
('Frozen Set:', frozenset(['e', 'g', 'f']))

Note: Frozensets are immutable, so methods like add() or remove() cannot be used. They are also hashable, which allows them to be used as dictionary keys.

Internal working of Set

Python sets use a hash table to store unique elements and perform insertion, deletion, and lookup efficiently. Python calculates a hash value to determine each element's initial position in the table.

  • Insertion: Checks whether an equal element already exists. If not, the element is added to an available slot.
  • Collision Handling: If two elements map to the same position, CPython uses open addressing with probing to find another suitable slot.
  • Lookup: Uses the hash value to locate the element and check for a match.
  • Deletion: Marks the removed slot with a special dummy marker to ensure that searches for other elements continue correctly.

Methods for Sets

1. Adding elements: add() function is used to insert new elements into a set. It automatically ignores duplicates.

Python
s = {"a", "b", "c"}
s.add("d")
print(s)

Output
{'c', 'd', 'a', 'b'}

2. Union: union() function combines two sets and returns a new set with all unique elements.

Python
a = {"x", "y"}
b = {"y", "z"}
u = a.union(b)
print(u)

Output
{'z', 'y', 'x'}

3. Intersection: intersection() function returns a new set containing elements that are common to both sets.

Python
a = {1, 2, 3}
b = {2, 3, 4}
i = a.intersection(b)
print(i)

Output
{2, 3}

4. Difference: difference() function returns a set containing elements that are in the first set but not in the second.

Python
a = {1, 2, 3}
b = {2, 3, 4}
d = a.difference(b)
print(d)

Output
{1}

5. Clear: clear() function removes all elements from a set, leaving it empty.

Python
s = {1, 2, 3}
s.clear()
print(s)

Output
set()

Operators for Sets

Sets and frozen sets support the following operators:

OperatorsNotes
key in scontainment check
key not in snon-containment check
s1 == s2s1 is equivalent to s2
s1 != s2s1 is not equivalent to s2
s1 <= s2s1 is subset of s2
s1 < s2s1 is proper subset of s2
s1 >= s2s1 is superset of s2
s1 > s2s1 is proper superset of s2
s1 | s2the union of s1 and s2
s1 & s2the intersection of s1 and s2
s1 - s2the set of elements in s1 but not s2
s1 ˆ s2the set of elements in precisely one of s1 or s2
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.