Set manipulation
- Set is an unordered collection of items. It means no indexing
- Set elements are unique and immutable.
- Set is mutable(able to perform add and update)
- Support heterogeneous type
- background data structure is hash table
- Set is used to perform mathematical operation like union,intersection, symmetric difference
Declaration:
placing all the items (elements) inside curly braces {}, separated by comma, or by using the built-in set() function. set(1,3,5”ab”)
empty set declaration
s = {()}
s = set()
Example:
s = {()}
print(type(s))
s = set()
print(type(s))
#unique and heterogeneous and unorderes
s = {22,44,11,"Abc",33,22,(77,99)}
print(s)
#datastructure is hashtable and elements are unique and immutable
#s = {22,44,11,"Abc",33,22,[77,99]}
print(s)
#s = {22,44,11,"Abc",33,22,{77:99}}
print(s)
Changing Set:
We are not able to access and change the elements from set because it is unordered collection so indexing is not working.
But we can add elements into set using add() (single ele) and update() (multiple ele)method
s = {1,4,2}
print(s)
s.add(22)
print(s)
s.update([11,33,44])
print(s)
s.update([11,33,44],{1,3,4},"avdgdh",{"ss":333})
print(s)
s.update([11,33,44],{1,3,4})
print(s)
Removing Set Element:
remove()-
- This method removes a given element if present otherwise it will throw an error.discard()
- This method removes a given element if it is not present, not throws an error.pop()
- This method removes elements in arbitrary order and returns that removed element.clear()
- This method clear all set elements but set is in memory
#removing elements from set
s = {22,44,66,44,33,22}
print(s)
#if element present
s.remove(44)
print(s)
#if element not present
#s.remove(31)
#print(s) #KeyError: 31
#2.discard() method
#removing elements from set
s = {22,44,66,44,33,28}
print(s)
#if element present
s.discard(44)
print(s)
#if element not present
s.discard(31)
print(s)
#3.pop() method
s = {22,44,66,44,33,28}
print(s)
print(s.pop())
#4.clear() method
s = {22,44,66,44,33,28}
print(s)
print(s.clear())
print(s)
Clone operation
Using copy() #Shallow Copy
- If you copy elements using this method from e1 variable to e2 and if you change element from e2 that will not be reflected in e1.
e1 = {22,44,66,44,33,28}
e2 = e1.copy()
print(e1)
print(e2)
e2.add(88)
print(e1)
print(e2)
print(id(e1))
print(id(e2))
2.Using = operator #deep copy
- If you copy elements using = from e1 variable to e2 and if you change element from e2 that will be reflected in e1.
e1 = {22,44,66,44,33,28}
e2 = e1
print(e1)
print(e2)
e2.add(88)
print(e1)
print(e2)
print(id(e1))
print(id(e2))
Mathematical Operation:
1. Union or | operator
- It combines two sets and returns a single set.
s1 = {2,3,1,4,5}
s2 = {3,2,6,7,8}
print(s1.union(s2))
print(s2.union(s1))
print(s1|(s2))
print(s2|s1)
2. Intersection or & operator
- It returns only common elements from both sets.
s1 = {2,3,1,4,5}
s2 = {3,2,6,7,8}
print(s1.intersection(s2))
print(s2.intersection(s1))
print(s1&(s2))
print(s2&s1)
3. Difference or - operator
If you difference on set1 and set2 i.t set1-set2 then this will return only those that are in set1 but not in set2.
s1 = {2,3,1,4,5}
s2 = {3,2,6,7,8}
print(s1.difference(s2))
print(s2.difference(s1))
print(s1-s2)
print(s2s1)
4. symmetricDifference or ^ operator
This will return only those elements that are not in both.
s1 = {2,3,1,4,5}
s2 = {3,2,6,7,8}
print(s1.symmetric_difference(s2))
print(s2.symmetric_difference(s1))
print(s1^s2)
print(s2^s1) print(s2^s2)
Frozen Set:
Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned.
# Frozensets
# initialize
a = frozenset([1, 2, 3, 4]) #When try to update we will get error AttributeError: 'frozenset' object has no attribute 'update'