Set Methods in Python
'''
Set Methods in Python:
Sets in Python more or less work in the same work as sets in mathematics.
We can operations like union and intersection on the sets just like in mathematics.
'''
'''
1. union() and update()
The union() and update() methods prints all the items that are present in the two sets.
The union() method returns a new set.
The update() method adds items into the existing set from an another set.
'''
s1 = {1, 7, 9, 3}
s2 = {5, 3, 8, 9}
print(s1.union(s2)) #output = {1, 3, 5, 7, 8, 9}
print(s1, s2) #output = {1, 3, 9, 7} {8, 9, 3, 5}
#Both the sets remains untouched
s1.update(s2)
print(s1, s2) #output = {1, 3, 5, 7, 8, 9} {8, 9, 3, 5}
#Here s1 is updated with the values that s1 doesn't have, but s2 has.
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.union(cities2)
print(cities3) #output = {'Delhi', 'Berlin', 'Madrid', 'Seoul', 'Kabul', 'Tokyo'}
cities1.update(cities2)
print(cities1, cities2)
#outut = {'Kabul', 'Tokyo', 'Seoul', 'Delhi', 'Madrid', 'Berlin'} {'Kabul', 'Seoul', 'Madrid', 'Tokyo'}
'''
2. Intersection and Intersection_update()
These two methods print only those items that are similar to both the sets.
The intersection () method returns a new set whereas intersection_update() method updates the existing set from another set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.intersection(cities2)
print(cities3) #output = {'Madrid', 'Tokyo'}
cities1.intersection_update(cities2)
print(cities1) #output = {'Madrid', 'Tokyo'}
'''
3. Symmetric_Difference and Symmetric_Differenece_Update()
Both methods print only the items that are not similar in both the sets.
The symmetric_difference() method returns a new set.
The symmeteic_difference_update() method updates into the existing set from another set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.symmetric_difference(cities2)
print(cities3) #output = {'Kabul', 'Seoul', 'Berlin', 'Delhi}
cities1.symmetric_difference_update(cities2)
print(cities1) #output = {'Seoul', 'Kabul', 'Berlin', 'Delhi'} #Order does not matter
'''
4. difference() and difference_update()
Both the methods print only the items that are only present in the original set and not in the both the sets.
The difference() method returns a new set.
The difference_update() method updates into the existing set from another set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.difference(cities2)
print(cities3) #output = {'Delhi', 'Berlin'}
cities1.difference_update(cities2)
print(cities1) #output = {'Delhi', 'Berlin'}
#There are several in-built methods used for the manipulation of set.
'''
5. isdisjoint()
This method checks if the items of the given set are present in another set.
It returns False if items are present, else it returns True.
In mathematics two sets are said to be disjoint sets, if if they have no elements in common.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.isdisjoint(cities2)
print(cities3) #output = False
cities1 = {"Tokyo2", "Madrid2", "Berlin2", "Delhi2"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.isdisjoint(cities2)
print(cities3) #output = True
'''
6. issuperset()
This method checks if all the items of a particular set are present in the original set.
It returns True if all the items are present, else it returns False.
'''
cities1 = {"Tokyo2", "Madrid2", "Berlin2", "Delhi2"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.issuperset(cities2)
print(cities3) #output = False
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Madrid"}
cities3 = cities1.issuperset(cities2)
print(cities3) #output = True
'''
7. issubset()
This method checks if all the items of the original set are present in the particular set.
It returns True if all the items are present, else it returns False.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.issubset(cities2)
print(cities3) #output = False
cities1 = {"Tokyo", "Madrid"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities3 = cities1.issubset(cities2)
print(cities3) #output = True
'''
8. add()
This method is used if we want to add a single item to the set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities1.add("Mumbai")
print(cities1) #output = {'Tokyo', 'Delhi', 'Mumbai', 'Berlin', 'Madrid'}
'''
9. update()
If we want to add more than one item, simply create another set or any other iterable object(list, tuple, dictionary), and use update() method to add it into the existing set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities1.update(cities2)
print(cities1) #output = {'Delhi', 'Madrid', 'Seoul', 'Kabul', 'Tokyo', 'Berlin'}
name1 = {"Ram", "Shyam", "Mohan"}
name2 = ["Pooja", "Leeja", "Roja"]
name1.update(name2)
print(name1) #output = {'Ram', 'Shyam', 'Mohan', 'Leeja', 'Roja', 'Pooja'}
'''
10. remove() or discard()
These methods are used to remove items from the list.
But the main difference between remove and discard is that, if we try to delete any item which is not present in the set, then remove() raises an error, but discard does not raise any error.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities1.remove("Madrid")
print(cities1) #output = {'Delhi', 'Tokyo', 'Berlin'}
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities1.discard("Madrid")
print(cities1) #output = {'Delhi', 'Tokyo', 'Berlin'}
# cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
# cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
# cities1.remove("Mumbai")
# print(cities1) #output = Keyerror: 'Mumbai'
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Kabul", "Seoul", "Madrid"}
cities1.discard("Mumbai")
print(cities1) #output = {'Berlin', 'Tokyo', 'Madrid', 'Delhi}
'''
11. pop()
This method removes the last item of the set, but the catch is that we do not know which item gets popped as sets are unordered.
However, we can access the popped item if we assign the pop() method to a variable.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
item = cities1.pop()
print(cities1) #output = {'Berlin', 'Tokyo', 'Delhi}
print(item) #output = Madrid
# 12.del
# It is not a method, rather it is a keyword which deletes the set entirely.
# cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
# del cities1
# print(cities1) #output = NmaeError: name 'cities1' is not defined
# Error because our entire set has been deleted and there is no variable called cities1 which contains a set.
#If we don't want to delete the entire set,but all items within the set, we use clear().
'''
13.clear()
This method clears all the items of the entire set and prints an empty set.
'''
cities1 = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities1.clear()
print(cities1) #output = set()
#How to check if an item is present in a set?
info = {"Carla", 19, False, 5.9}
if "Carla" in info:
print("Carla is present")
else:
print("Carla is absent")
#output = Carla is present
Comments
Post a Comment