Set functionality:
- ‘set’ is a class.
- ‘set’ object providing pre-defined functions to process the elements
s = {10,20,30,40,50}
print("Set :",s)
s.add(60)
print("Set after adding :",s)
s.discard(20)
s.discard(40)
print("Set after deleting :",s)
Output:
Set : {40, 10, 50, 20, 30}
Set after adding : {40, 10, 50, 20, 60, 30}
Set after deleting : {10, 50, 60, 30}
s.clear()
print(s)
We can construct the collection of object by calling constructor.
my_list = list()
my_set = set()
- We can construct one collection object by passing another collection as an input.
- If we pass List as input to Set, all duplicates will be removed.
my_list = [10, 20, 30, 20, 10]
print("List is :", my_list)
my_set = set(my_list)
print("Set is :", my_set)
Output:
List is : [10, 20, 30, 20, 10]
Set is : {10, 20, 30}