Internal working of Set in Python
A Set in Python can be defined as a collection of unique items. It is an unordered collection that does not allow duplicate entries. Sets are primarily used for membership testing and eliminating duplicates from a sequence. The underlying data structure used in sets is Hashing, which allows insertion, deletion, and traversal operations in O(1) on average. Operations on Hash Tables are similar to those on Linked Lists, and sets in Python are unordered collections with duplicate elements removed.
Basic Methods on Sets
1. Creating a Set
In Python, sets are created using the set() function. This creates an empty set. It's important to note that an empty set cannot be created using {}, as it will create an empty dictionary instead.
x = set() # Creates an empty set
2. Checking if an Item is in a Set
To check if an item exists in a set, you can use the in keyword. The average time complexity for this operation is O(1), but in the worst case, it can become O(n).
'pear' in fruit # Fast membership testing.
3. Adding Elements
Elements are added to a set using the add() function. The insertion in a set is efficient, with an average time complexity of O(1). However, in the worst case, it can be O(n).
A.add('h') # Adds 'h' to the set
4. Union
The union of two sets can be performed using the union() method or the | operator. The union operation combines elements from both sets while removing duplicates. The time complexity is O(len(s1) + len(s2)), where s1 and s2 are the two sets being merged.
A | B # Union of sets A and B
5. Intersection
To find the common elements between two sets, you can use the intersection() method or the & operator. The time complexity for this operation is O(min(len(s1), len(s2))), where s1 and s2 are the sets being compared.
A & B # Intersection of sets A and B
6. Difference
The difference between two sets can be found using the difference() method or the - operator. The time complexity for this operation is O(len(s1)), where s1 is the set from which elements are being subtracted.
A - B # Elements in A but not in B
7. Symmetric Difference
The symmetric difference between two sets is the set of elements that are in either of the sets but not in both. This operation can be done using the symmetric_difference() method or the ^ operator. The time complexity is O(len(s1)).
A ^ B # Symmetric difference between sets A and B
8. Symmetric Difference Update
The symmetric_difference_update() method modifies the set in place to contain the symmetric difference of the two sets. The time complexity for this operation is O(len(s2)).
A.symmetric_difference_update(B)
9. Clear
The clear() method removes all elements from the set, effectively making it an empty set. This operation has a time complexity of O(n).
A.clear() # Clears all elements from set A
Set Implementation
If Multiple values are present at the same index position, then the value is appended to that index position, to form a Linked List. In, Python Sets are implemented using dictionary with dummy variables, where key beings the members set with greater optimizations to the time complexity. Set Implementation:

Sets with Numerous operations on a single HashTable:

Examples of Set Operations
Here are some examples demonstrating how sets work in Python:
Creating Sets:
x = set()
print(x)
B = set('hello')
print(B)
Output
set() {'h', 'o', 'l', 'e'}
Adding Elements:
A = set('abcdefg')
print(A)
A.add('h')
print(A)
Output
{'a', 'g', 'b', 'f', 'c', 'e', 'd'} {'a', 'g', 'h', 'b', 'f', 'c', 'e', 'd'}
Membership Testing:
fruit = {'orange', 'banana', 'pear', 'apple'}
print('pear' in fruit)
print('mango' in fruit)
Output
True False
Set Operations:
A = set('abcdefg')
B = set('hello')
print(A == B)
print(A != B)
print(A <= B)
print(A < B)
print(A > B)
print(A | B)
print(A & B)
print(A - B)
print(A ^ B)
Output
False True False False False {'g', 'c', 'h', 'e', 'o', 'd', 'l', 'b', 'a', 'f'} {'e'} {'a', 'g', 'd', 'b', 'c', 'f'} {'g', 'c', 'o', 'l', 'd', 'h', 'a', 'f', 'b'}
Set Comprehension:
A = set('abcdefg')
a = {x for x in A if x not in 'abc'}
print(a)
Output
{'g', 'f', 'd', 'e'}
Related Articles: