How Python Sets Work: The Power of Hashing and Uniqueness

Sets
When we hear the word "set," we often think about sets in mathematics β a collection of distinct, well-defined elements. Python adopts the same concept, but as a built-in data type and data structure.
If you're not familiar with how Python sets work, donβt worry β I'll explain it clearly.
What is a Set in Python?
A set in Python is an unordered collection of well-defined, unique, and hashable objects. Sets are defined using curly braces {}, and elements inside are separated by commas.
a = {1, 2, 3, 4}
π Key Properties of Python Sets:
Unique Elements
Sets automatically remove duplicates β every element is stored only once.Unordered
Unlike lists and tuples, sets have no index β elements do not have a fixed position.Hashable Elements Only
Sets only allow hashable (i.e., immutable) items like numbers, strings, or tuples. You can't add mutable types like lists or dictionaries to a set.Mutable Container
While the elements must be hashable, the set itself is mutable β you can add or remove elements any time using methods like.add()or.remove().
How to Convert another hash sequence datatype in sets ?
to do that we use function set( ).

Set only store immutable datatypes
hash
hash is a fixed sized number that is calculated from data.
hashable
hashable means an object that have hash value , immutable.
unhashable
hashable means an object that do not have hash value , mutable.
benefits of hashable element
A set can only store elements that are hashable.
When you add an element to a set, Python calculates its hash value and stores it based on that value.
When you want to check if an element is present in a set (a membership test), Python uses the hash value to jump directly to the location where that element would be β making the lookup very fast.

how its internally work you can understand using image given bellow.

In contrast, a list checks each element one by one during a membership test, which is much slower for large datasets.
when we use sets
when we want to check membership because that operation is very fast on set compared to list, tuple or etc.
when we want a sequence whose elements are unique.
when we want to perform set operation on a sequence.
Python Set Methods
1. add( )
add a given no to a set.

2. remove( )
remove given element from set.

3. clear( )
delete all elements of set and return an empty set.

4. del
delete the set and when we call set raise error and say not defined.

5 . pop( )
return and remove random element from set.

set operations
for understanding some method we have to same knowledge about sets operation and also along with this we cover how this operation is done using python sets method
union
used to add two set.

6. Union( )
return union of sets not store the final answer of union.

intersection
used two find same elements between two sets.

7 . intersection( )
return intersection of sets not store the final answer of intersection.

8 . intersection_update( )
store the final answer of intersection in set a.

difference of set
all elements of set a which are not present in set b.

9 . difference( )

10 . difference_update( )
store the final answer of difference in set a.

symmetric difference
all elements of set A and set B except common elements.

11 . symmetric_difference( )

12 . symmetric_difference_update( )
store the final answer of symmetric difference in set a.

13 . isdisjoint( )
check two set have different elements and return True or False.

14 . issubset, issuperset( )
check a is subset of another set and return True or False.
subset = a is subset of b, if b contain all or more elements that are in set a.
for ex- A={1,2,3} B = {2,1,3,4,5,6} A is subset of b and B is superset of A.
superset = B is superset of B if B contain all or more elements that are present in A.

#chaicode



