
Sets in Python are unordered, immutable, unindexed collection of elements enclosed under { } (curly braces).
Since, Python Sets are unordered so you can not predict in which order elements will appear also elements in Sets are unindexed hence, you can not access the set elements via index.
Python Sets are generally used to store different values of different types.
Python has a specific keyword for defining sets -> set().
s = {} # empty set
s = {1, 'hello', 'word', (3, 5)} # set with values
Please note that, we can not add list (or a dictionary, even a set) as an element of the set. This is due to the fact that set is an unordered collection of distinct hashable objects.
Sets in Python does not allow duplicate values.
s = {'hello', True, 'word', 'hello'} # set with duplicate values
print(s)
O/P
{True, ‘hello’, ‘word’}
Once a set is created, we cannot modify its elements, but can add new items.
Please note that, Python Sets does not support slicing operation.
How Python Sets are stored in memory
Internal working of Python Sets is based on the concept of Hash Table. Python Set uses dictionary like implementation as the index-value pair.
This is the reason why Python Sets are great in terms of performance. Like, the complexity of Sets for basic operations like, fetching an element, adding an element, removing an element is always O(1) which is not the case in Python Lists.
Length of a Python Set
To find the length of a Python Set, we can use len() function.
s = {'hello', True, 'word'} # set
print(len(s))
O/P
3
Accessing elements of a Python Set
In Python Sets, we can not access elements by its index like we generally do in Python lists, string or tuple. Rather, we have the option to loop over the set elements using in statement. Like,
s = {'hey', 'we', 'are', 'humans', 'born', 'in', 2022}
for element in s:
print(element, end= ' ')
O/P
we in 2022 are hey humans born
Observe, order of set elements is not being maintained internally.
Add Elements in Python Sets
The add() method in Python Sets can be used to add an element in the given set.
Syntax
set.add(element)
Let’s try to add new element (string type) to the given set,
s = {'one', 2, 3, 'two', 'four'}
print('Before addition : ', s)
s.add('val') # adding val to given set s
print('After addition : ', s)
O/P
Before addition : {2, 3, ‘two’, ‘four’, ‘one’}
After addition : {2, 3, ‘two’, ‘four’, ‘val’, ‘one’}
Add Multiple Elements to a Set in Python
The update() method in Python Sets can be used to add multiple values to a given Python Set.
Syntax
set.update(object)
The object inside the update() method can be any iterable object (sets, tuples, lists, dictionaries etc).
Hence, with update() method, we can :
1. Add a set to a given set
2. Add a list to a given set
3. Add a tuple to a given set
4. Add a dictionary to a given set
s = {1, 2, 3, 4}
to_add = {'one', 'two', 'three', 'four'}
print('Before addition : ', s)
s.update(to_add) # adding set to_add in s
print('After addition : ', s)
O/P
Before addition : {1, 2, 3, 4}
After addition : {‘three’, 1, 2, 3, 4, ‘one’, ‘four’, ‘two’}
Please note that, adding an element in Set is an optimized process, with time complexity a O(1).
Other Operations in Python Sets
Remove Elements in Python Sets
Union in Python Sets
Intersection in Python Sets
Please go through the complete tutorials on Python Sets.
