-
Notifications
You must be signed in to change notification settings - Fork 41
/
remove-set-elements.py
55 lines (43 loc) · 1.73 KB
/
remove-set-elements.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# ------------------------------------------------------------------------------------
# Tutorial: Removing Elements in a Set
# A particular item can be removed from a set using the methods discard() and remove().
# ------------------------------------------------------------------------------------
# The only difference between the two is that the discard() function leaves a set unchanged if the element is not present in the set.
# On the other hand, the remove() function will raise an error in such a condition (if element is not present in the set).
# Difference between discard() and remove()
# initialize my_set
my_set = {1, 3, 4, 5, 6}
print(my_set)
# discard an element
# Output: {1, 3, 5, 6}
my_set.discard(4)
print(my_set)
# remove an element
# Output: {1, 3, 5}
my_set.remove(6)
print(my_set)
# discard an element
# not present in my_set
# Output: {1, 3, 5}
my_set.discard(2)
print(my_set)
# Similarly, we can remove and return an item using the pop() method.
# Since set is an unordered data type, there is no way of determining which item will be popped. It is completely arbitrary.
# We can also remove all the items from a set using the clear() method.
# initialize my_set
# Output: set of unique elements
my_set = set("HelloWorld")
print(my_set)
# pop an element
# Output: random element
print(my_set.pop())
# pop another element
my_set.pop()
print(my_set)
# clear my_set
# Output: set()
my_set.clear()
print(my_set)
# ------------------------------------------------------------------------------------
# Challenge: Remove an element which is not present in my_set and print the set. Observe the output. Check which type of error it is and why it occurs.
# ------------------------------------------------------------------------------------