


Khusboo Tayal
In Python, a set is an unordered collection of unique elements. Sets are useful when you want to store non-duplicate values and perform operations like union, intersection, and difference.
This guide will help you understand how to create, manipulate, and use sets effectively in Python programming.
A set is a built-in data type that holds an unordered and unindexed collection of unique elements. Sets are defined using curly braces {} or the built-in set() function.
fruits = {"apple", "banana", "cherry"}Duplicate values will be automatically removed from a set.
colors = {"red", "green", "blue"}numbers = set([1, 2, 3, 4, 5])
Note: An empty set must be created with set(), not {} (which creates an empty dictionary).
empty_set = set()
Sets are unordered, so you cannot access items by index. However, you can loop through the set using a for loop:
for color in colors:
print(color)Use the add() method to add a single item:
colors.add("yellow")Use update() to add multiple items:
colors.update(["orange", "black"])
colors.remove("red")
colors.discard("green")
colors.pop()
colors.clear()
Python provides built-in methods for performing mathematical set operations:
Combines elements from both sets.
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # {1, 2, 3, 4, 5}Returns common elements.
print(a & b) # {3}Returns elements present in the first set but not in the second.
print(a - b) # {1, 2}
Returns elements in either set but not in both.
print(a ^ b) # {1, 2, 4, 5}| Method | Description |
|---|---|
| add() | Adds a single element |
| update() | Adds multiple elements |
| remove() | Removes an element, raises error if not found |
| discard() | Removes an element, no error if not found |
| pop() | Removes and returns a random element |
| clear() | Removes all elements |
| union() | Combines sets |
| intersection() | Returns common elements |
| difference() | Elements only in the first set |
| symmetric_difference() | Elements in either but not both |
A frozenset is an immutable version of a regular set. Once created, you cannot modify it (no add or remove operations).
fs = frozenset([1, 2, 3])
nums = [1, 2, 2, 3, 4, 4, 5]
unique_nums = set(nums)
print(unique_nums) # {1, 2, 3, 4, 5}
Python sets are an excellent way to handle collections of unique items. With powerful built-in methods, you can easily perform set operations like union, intersection, and difference.
Understanding sets is crucial when working with data filtering, uniqueness, and fast membership testing in Python.
Q1: Can sets contain duplicate elements?
No, sets automatically eliminate duplicate values.
Q2: Are sets ordered?
No, sets are unordered collections.
Q3: What is the difference between remove() and discard()?
remove() throws an error if the element is not found, while discard() does not.
Subscribe to our newsletter to get the latest Python tutorials, project ideas, and updates right to your inbox.
#python #pythonsets #pythondatatypes #setoperations #pythonforbeginners #learnpython #pythontutorial #uniquedata #frozenset #pythonexamples