Sets | Dataplexa

Sets in Python

A set in Python is an unordered collection of unique elements. This means:

  • No duplicate values are allowed
  • Order is not guaranteed
  • Elements must be immutable (numbers, strings, tuples)

Sets are extremely useful when you need to store distinct values, remove duplicates, perform mathematical operations (union, intersection), or check membership efficiently.

What Is a Set?

A set uses { } curly brackets and automatically removes duplicate items.

my_set = {1, 2, 3, 3, 4}
print(my_set)      # {1, 2, 3, 4}

Notice that the duplicate 3 appears only once — sets keep only unique values.

Why Use Sets?

Sets are helpful because they provide:

  • Automatic duplicate removal
  • Fast membership testing (in operator)
  • Clean mathematical operations
  • Better performance in lookups compared to lists
  • Useful tools for handling unique items in datasets

Creating Sets

numbers = {10, 20, 30}
empty_set = set()      # correct way
wrong_empty = {}       # this is a dictionary!

Accessing Set Elements

Sets do not support indexing or slicing because they are unordered. You must use a loop to access elements.

fruits = {"apple", "banana", "mango"}

for x in fruits:
    print(x)

Adding and Removing Elements

You can add new elements using add() or update(). You can remove elements using remove(), discard(), or pop().

MethodDescription
add(x)Adds a single element
update(iterable)Adds multiple elements
remove(x)Removes element (error if not found)
discard(x)Removes element (no error if missing)
pop()Removes a random element
clear()Removes all elements
s = {1, 2, 3}

s.add(4)
s.update([5, 6])

s.remove(2)
s.discard(10)     # no error

s.pop()           # removes random item

Mathematical Set Operations

Python sets allow powerful mathematical operations that are used in data processing and analytics.

Union (combine elements)

a = {1, 2, 3}
b = {3, 4, 5}

print(a | b)           # {1, 2, 3, 4, 5}
print(a.union(b))      # same output

Intersection (common elements)

a = {1, 2, 3}
b = {2, 3, 4}

print(a & b)           # {2, 3}
print(a.intersection(b))

Difference (elements in A but not B)

a = {1, 2, 3}
b = {3, 4, 5}

print(a - b)           # {1, 2}
print(a.difference(b))

Symmetric Difference (elements not common)

a = {1, 2, 3}
b = {3, 4, 5}

print(a ^ b)          # {1, 2, 4, 5}
print(a.symmetric_difference(b))

Frozen Sets (Immutable Sets)

A frozenset is an immutable version of a set. You cannot add or remove elements after creation.

fs = frozenset([1, 2, 3])
print(fs)

Frozen sets are useful when you need a set that should never change, or when using sets inside other sets.

Real-World Example: Removing Duplicate Values

data = [10, 20, 20, 30, 30, 30, 40]

unique_values = set(data)
print(unique_values)

Real-World Example: Finding Common Skills

person1 = {"python", "sql", "ml"}
person2 = {"python", "excel", "sql"}

common = person1 & person2
print(common)

📝 Practice Exercises


  1. Create a set of 5 numbers and try adding a duplicate value. Observe the result.
  2. Write a program to find the union and intersection of two sets.
  3. Remove an element from a set using discard() without causing an error.
  4. Create a set from a list of repeated items and print the unique values.
  5. Use set difference to find items present in one set but not in another.

✅ Practice Answers


Answer 1:

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

Answer 2:

a = {1, 2, 3}
b = {3, 4, 5}

print(a.union(b))
print(a.intersection(b))

Answer 3:

s = {10, 20, 30}
s.discard(50)
print(s)

Answer 4:

data = [1, 1, 2, 3, 3, 4]
unique = set(data)
print(unique)

Answer 5:

a = {1, 2, 3}
b = {3, 4}

print(a - b)