-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremove_set_items.py
29 lines (25 loc) · 932 Bytes
/
remove_set_items.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Removing set items
# Note: remove method takes exactly only one argument.
fruits = {'apple', 'orange', 'watermelon', 'guvva'}
fruits.remove('orange') # remove method removes the specified value from the set
print(fruits)
# remove method throws an error if the specifed value doesn't exits.
# Discard method won't throw an error if the specifed value doesn't exits.
fruits = {'apple', 'orange', 'watermelon', 'guvva'}
fruits.discard('apple')
print(fruits)
'''
pop() method removes last value, but we don't know which value will be removed
beacuse sets are unordered.
'''
fruits = {'apple', 'orange', 'watermelon', 'guvva'}
fruits.pop()
print(fruits)
# del keyword deletes entire set
fruits = {'apple', 'orange', 'watermelon', 'guvva'}
del fruits
print(fruits) # NameError will raise because set is deleted
#clear() method empty's the set
fruits = {'apple', 'orange', 'watermelon', 'guvva'}
fruits.clear()
print(fruits)