Namaste Coders!
In this article, we will discover sets data structure in python. Let's take it together.
What are Sets ?
Sets are another datatype in python which doesn’t take in duplicate data in it. Basically it ignores duplicate data.
l=[1,2,3,4,52,1,2,5]
s=set(l)
print(s)
# {1, 2, 3, 4, 5, 52}
Set is an unordered collection and does not record element position or order of insertion. Sets do not support indexing, slicing, or other sequence-like behavior.
s={1,2,3,4,5,6}
print(s[3])
There are currently two built-in set types:
- set
- frozenset
Set
The set type is mutable the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.
Frozenset
The frozenset type is immutable and hashable . Its contents cannot be altered after it is created; it can, therefore, be used as a dictionary key or as an element of another set.
my_list = ['a', 'b', 'c', 'd']
frozen_set = frozenset(my_list)
print(frozen_set)
# frozenset({'b', 'c', 'd', 'a'})
Iteration in set
s={1,2,3,4,5,6}
for i in s:
print(i)
# 1
# 2
# 3
# 4
# 5
# 6
Adding element in set
We can either add single element using add() or multiple elements using update()
s={1,2,3,4,5,6}
s.add(7)
print("after adding 7",s)
s.update([25,75])
print(s)
output
after adding 7 {1, 2, 3, 4, 5, 6, 7} {1, 2, 3, 4, 5, 6, 7, 75, 25}
Removing Element
pop(), remove() and discard() functions are used to remove individual item from a Python set. See the following example
s={1,2,3,4,5,6}
# pops out element from front
s.pop()
print("after using pop function: ",s)
#output
# after using pop function: {2, 3, 4, 5, 6}
# doesnot gives error if element is not present
s.discard(15)
# remove function gives key error if element is not present
s.remove(15) #will through KeyError
Set Operations
- Intersection of Sets: It provides common elements in both sets.
- Union of Sets: It returns a set which contains elements of both sets.
- set difference: The difference() method returns a new set containing all the elements that are in set X but not set Y and vice versa.
- issubset and issuperset: This returns a boolean value that denotes if the set is superset or subset of the other set.
s1={1,2,3,4,5}
s2={3,4,6,76,85,5}
# intersection
print("intersection: ",s1.intersection(s2))
print("intersection: ",s1&s2)
# union
print("union: ",s1.union(s2))
print("union: ",s1|s2)
# set difference
print("s1-s2",s1-s2)
print("s1-s2",s1.difference(s2))
print("s2-s1",s2-s1)
# to check s1 is subset of s2
print('issubset: ',s1<=s2)
# clear the set
s1.clear()
s2.clear()
print(s1)
print(s2)
Output
after using pop function: {2, 3, 4, 5, 6} intersection: {3, 4, 5} intersection: {3, 4, 5} union: {1, 2, 3, 4, 5, 6, 76, 85} union: {1, 2, 3, 4, 5, 6, 76, 85} s1-s2 {1, 2} s1-s2 {1, 2} s2-s1 {76, 85, 6} issubset: False set() set()
The End
I hope you enjoyed the article and had a good learning experience.
Follow for more articles and keep sharing👩
Keep coding