s = {0, 1, 2}
s.discard(0)
print(s)
{1, 2}
# discard() does not throw an exception if element not found
s.discard(0)
# remove() will throw
s.remove(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 0
# it doesn't raise error if element doesn't exits in set
thisset = {1, 2,3}
thisset.discard(3)
print(thisset)
list1 = {1,2,3,4}
list1.remove(4)
print(list)
# {1,2,3}
list.discard(item)
s = set()
s.remove(x)
# Creating an empty set
b = set()
print(type(b))
## Adding values to an empty set
b.add(4)
b.add(4)
b.add(5)
b.add(5) # Adding a value repeatedly does not changes a set
b.add((4, 5, 6))
## Accessing Elements
# b.add({4:5}) # Cannot add list or dictionary to sets
print(b)
## Length of the Set
print(len(b)) # Prints the length of this set
## Removal of an Item
b.remove(5) # Removes 5 fromt set b
nameSet = {"John", "Jane", "Doe"}
nameSet.discard("John")
print(nameSet)
# {'Doe', 'Jane'}
mySet = {1, 2, 3}
mySet.remove(1)
print(mySet)
# Output:
# {2, 3}
nameSet = {"John", "Jane", "Doe"}
nameSet.remove("Jane")
print(nameSet)
# {'John', 'Doe'}