remove element from set code example

Example 1: python set remove

s = {0, 1, 2}
s.discard(0)  
print(s)
{1, 2}

# discard() does not throw an exception if element not found
s.discard(0)

# remove() will throw
s.remove(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 0

Example 2: remove an item from a set python

# it doesn't raise error if element doesn't exits in set

thisset = {1, 2,3}

thisset.discard(3)

print(thisset)

Example 3: remove element form set in python

list1 = {1,2,3,4}
list1.remove(4)
print(list)
# {1,2,3}

Example 4: remove an item from a set python

list.discard(item)

Example 5: remove from set javascript

// Create new set
const animals = new Set();

// Add to set
animals.add('cat');
animals.add('dog');

// Remove from set
animals.delete('cat'); // returns true
animals.delete('dog'); // returns true
animals.delete('horse'); // returns false

Example 6: remove an element from a set

list.remove(element)