Python Practice › Set Programs › Program 3
Remove an element from a set
Write a Python program to remove an element from a set.
Input
{10, 20, 30}Output
{10, 30}Approach
remove()raises KeyError if missing;discard()does not.
Solution
s = {10, 20, 30}s.remove(20)s.discard(99)print(s)Output
{10, 30}Learn the concept
New to this topic? Read the lesson, then come back.