delete 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: set.delete

// erasing from set
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator it;

  // insert some values:
  for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90

  it = myset.begin();
  ++it;                                         // "it" points now to 20

  myset.erase (it);

  myset.erase (40);

  it = myset.find (60);
  myset.erase (it, myset.end());

  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Example 4: remove an item from a set python

list.discard(item)

Example 5: remove an element from a set

list.remove(element)

Tags:

C Example