Sets in Python – Built-in Duplicate Bouncer


In the previous blog, we got to know about Python’s other data structure “Tuples↖︎”. In this blog, we will discuss “Set”.
Let’s get started.
What is a Python Set?
Imagine we’re organizing a party. We make a guest list, but realize… we accidentally wrote some names twice!
This is where sets come in — they automatically remove duplicates and help keep things neat.
In Python, a set is just a bag of unique items. No duplicates, and the order doesn’t matter.
Properties of a Set in Python
1. Unordered
Sets do not maintain any specific order of elements.
s = {3, 1, 4, 2}
print(s) # Output might be: {1, 2, 3, 4} or any order
2. No Duplicates Allowed
Sets automatically eliminate duplicate values.
s = {1, 2, 2, 3, 4, 4}
print(s) # Output: {1, 2, 3, 4}
3. Mutable (Changeable)
We can add or remove elements from a set.
Hashable:
Has a fixed (unchanging) value (i.e., it's immutable).
unhashable:
Unhashable means an object that do not have hash value , mutable. Set is unhashable.
s = {1, 2}
s.add(3) # Adds 3 to the set
s.remove(2) # Removes 2 from the set
print(s) # Output: {1, 3}
4. No Indexing or Slicing
Since sets are unordered, we can't access elements by index.
s = {1, 2, 3}
# s[0] → will raise TypeError
5. Supports Set Operations
We can do mathematical set operations like union, intersection so on.
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b) # Union: {1, 2, 3, 4, 5}
print(a & b) # Intersection: {3}
print(a - b) # Difference: {1, 2}
6. Dynamic Size
Sets can grow or shrink as elements are added or removed.
s = set()
s.add("apple")
s.add("banana")
s.remove("apple")
Python Built-in Functions:
Function | Description |
len() | Get the length of the iterable. |
type() | Get the type of object. |
str() , int() , float() | Convert to string, integer, or float. |
list() , set() , tuple() | Convert to list, set, tuple. |
range() | Create a range of numbers. |
sum() | Return the sum of iterable. |
max() , min() | Return the largest/smallest item. |
sorted() | Return a sorted list from an iterable. |
input() | Read input from the user. |
abs() | Return the absolute value. |
round() | Round a number. |
enumerate() | Return index-item pairs. |
zip() | Combine two or more iterables. |
map() | Apply a function to each item. |
filter() | Filter items using a condition. |
all() , any() | Return True if all/any are True. |
dir() | List attributes/methods of the object. |
help() | Show help for the object. |
Let’s explore all the set methods in python:
Let’s explore properties of List, Tuple, Set in Python:
Property | List | Tuple | Set |
Mutability | Mutable | Immutable | Mutable (elements must be immutable) |
Indexing | Supports indexing | Supports indexing | No indexing |
Duplicates | Allows duplicates | Allows duplicates | No duplicates |
Order | Ordered (since Python 3.7) | Ordered | Ordered (since Python 3.7)* |
Syntax | [1, 2, 3] | (1, 2, 3) | {1, 2, 3} |
Here are some code snippets in Colab about Python List:
Conclusion on Python Sets:
Python sets are powerful, built-in data structures used to store unordered, mutable, and unique elements. They are ideal for operations involving membership testing, removing duplicates, and performing standard mathematical set operations such as union, intersection, and difference.
With a rich collection of built-in methods, sets make tasks like filtering, combining datasets, and comparing collections efficient and readable. Their performance and simplicity make them a valuable tool for any Python programmer.
Hope you enjoyed reading this blog and gained some useful insights from it!
Subscribe to my newsletter
Read articles from Anindita Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
