🚀 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...

set()

AI & DATA SCIENCE // set

set() builds a new mutable, unordered collection of unique elements, either empty or from the items of any iterable.

Syntax

set()
set(iterable)

Deep Dive Course

set() removes duplicates from whatever iterable you pass it, since a set can only contain each distinct value once, and it requires every element to be hashable, so lists and dicts can't be set members, but tuples and strings can. Sets support fast membership testing in average constant time, and mathematical operations like union, intersection, and difference.

1Understanding set()

set() removes duplicates from whatever iterable you pass it, since a set can only contain each distinct value once, and it requires every element to be hashable, so lists and dicts can't be set members, but tuples and strings can. Sets support fast membership testing in average constant time, and mathematical operations like union, intersection, and difference.

💡

Calling set() with no arguments makes an empty set — but a pair of empty curly braces makes an empty dict instead, because that syntax was already reserved for dict literals before set literals existed.

editor.html
numbers = [1, 2, 2, 3, 3, 3]
unique = set(numbers)
print(unique)
localhost:3000

2Practical Example

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

editor.html
admins = {"alice", "bob"}
online = {"bob", "carol"}
print(admins & online)
print(admins | online)
localhost:3000

3Best Practices

Follow these guidelines when working with set():

1. Use set(iterable) to quickly deduplicate a list while discarding order

2. Use set intersection/union/difference operators instead of manual loops when comparing two collections

3. Remember sets are unordered — sort the result if you need a predictable, ordered output

⚠️

Tip: Calling set() with no arguments makes an empty set — but a pair of empty curly braces makes an empty dict instead, because that syntax was already reserved for dict literals before set literals existed.

editor.html
numbers = [1, 2, 2, 3, 3, 3]
unique = set(numbers)
print(unique)
localhost:3000

Examples

Example 01Basic Usage
numbers = [1, 2, 2, 3, 3, 3]
unique = set(numbers)
print(unique)
Example 02Advanced Example
admins = {"alice", "bob"}
online = {"bob", "carol"}
print(admins & online)
print(admins | online)

Best Practices

  • Use set(iterable) to quickly deduplicate a list while discarding order
  • Use set intersection/union/difference operators instead of manual loops when comparing two collections
  • Remember sets are unordered — sort the result if you need a predictable, ordered output

Interview Question

Why can't you put a list inside a set, but you can put a tuple?

Hint: Think about mutability and hashing.

Set membership relies on each element's hash value, computed once when it's added, to place it into the right bucket for fast lookup. Lists are mutable, so their contents, and thus their hash, could change after insertion, which would corrupt the set's internal structure — so Python simply makes lists unhashable, raising a TypeError. Tuples are immutable, so as long as all their elements are also hashable, a tuple itself is hashable and can be stored in a set.

Exercises

MediumPractice using set() in a real scenario.
View Solution
numbers = [1, 2, 2, 3, 3, 3]
unique = set(numbers)
print(unique)

Frequently Asked Questions

Why can't you put a list inside a set, but you can put a tuple?

Set membership relies on each element's hash value, computed once when it's added, to place it into the right bucket for fast lookup. Lists are mutable, so their contents, and thus their hash, could change after insertion, which would corrupt the set's internal structure — so Python simply makes lists unhashable, raising a TypeError. Tuples are immutable, so as long as all their elements are also hashable, a tuple itself is hashable and can be stored in a set.

Related Functions

frozenset()dictionariesin operator