Removing Elements from a Set

Tags: removal, Set

Write a Python program to remove a member from a set and return the modified set. If the set doesn’t contain the element, return the original set.

Example 1:

Input: {1, 2, 3, 4, 5}, 3 
Output: {1, 2, 4, 5}

Example 2:

Input: {5, 7, 8, 9, 10}, 11 
Output: {5, 7, 8, 9, 10}

Use the discard() function to remove an element from a set.

def remove_from_set(s, el):
    s.discard(el)
    return s

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!