🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Sets

AI & DATA SCIENCE // sets

A set is Python's built-in, mutable, unordered collection of unique, hashable elements, optimized for fast membership testing and set algebra.

Syntax

s = {1, 2, 3}
s.add(4)
1 in s

Deep Dive Course

Sets are backed by the same hash-table machinery as dictionaries, so checking whether a value is in a set runs in average O(1) time regardless of the set's size, unlike a list where membership testing is O(n). Sets also implement the standard mathematical set operations — union, intersection, difference, and symmetric difference — as both operators and named methods.

1Understanding Sets

Sets are backed by the same hash-table machinery as dictionaries, so checking whether a value is in a set runs in average O(1) time regardless of the set's size, unlike a list where membership testing is O(n). Sets also implement the standard mathematical set operations — union, intersection, difference, and symmetric difference — as both operators and named methods.

💡

Swap a list for a set when you're doing a lot of membership checks in a loop — it turns an O(n) scan into an O(1) lookup on average.

editor.html
seen = set()
for n in [1, 2, 2, 3, 1]:
    if n not in seen:
        seen.add(n)
print(seen)
localhost:3000

2Practical Example

Here is a real-world application of Sets showing how it is used in production Python code.

editor.html
required = {"id", "name", "email"}
provided = {"id", "name"}
missing = required - provided
print(missing)
localhost:3000

3Best Practices

Follow these guidelines when working with Sets:

1. Use a set instead of a list when you need fast membership testing and don't care about order or duplicates

2. Use set operators (|, &, -) instead of manual loops when comparing two collections

3. Use frozenset instead of set when the collection needs to be hashable itself, e.g. as a dictionary key

⚠️

Tip: Swap a list for a set when you're doing a lot of membership checks in a loop — it turns an O(n) scan into an O(1) lookup on average.

editor.html
seen = set()
for n in [1, 2, 2, 3, 1]:
    if n not in seen:
        seen.add(n)
print(seen)
localhost:3000

Examples

Example 01Basic Usage
seen = set()
for n in [1, 2, 2, 3, 1]:
    if n not in seen:
        seen.add(n)
print(seen)
Example 02Advanced Example
required = {"id", "name", "email"}
provided = {"id", "name"}
missing = required - provided
print(missing)

Best Practices

  • Use a set instead of a list when you need fast membership testing and don't care about order or duplicates
  • Use set operators (|, &, -) instead of manual loops when comparing two collections
  • Use frozenset instead of set when the collection needs to be hashable itself, e.g. as a dictionary key

Interview Question

Why is checking membership in a set faster than checking membership in a list?

Hint: Think about how each data structure is stored in memory.

A list stores its elements in a plain sequence, so checking whether a value is present means comparing it against each element one by one in the worst case, an O(n) scan. A set is backed by a hash table: it computes the value's hash and jumps almost directly to the bucket where it would live, making membership testing average O(1) regardless of how many elements the set holds.

Exercises

MediumPractice using Sets in a real scenario.
View Solution
seen = set()
for n in [1, 2, 2, 3, 1]:
    if n not in seen:
        seen.add(n)
print(seen)

Frequently Asked Questions

Why is checking membership in a set faster than checking membership in a list?

A list stores its elements in a plain sequence, so checking whether a value is present means comparing it against each element one by one in the worst case, an O(n) scan. A set is backed by a hash table: it computes the value's hash and jumps almost directly to the bucket where it would live, making membership testing average O(1) regardless of how many elements the set holds.

Related Functions

frozensetsdictionarieslist()