Python Practice › Set Programs › Program 3

Remove an element from a set

EasySet Programs · 3 of 48

Write a Python program to remove an element from a set.

Advertisement
Input
{10, 20, 30}
Output
{10, 30}

Approach

  1. remove() raises KeyError if missing; discard() does not.

Solution

Python
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.

More set programs

← All 48 Set Programs

Leave a Comment