Answers for "delete and return element from set python"

7

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
Posted by: Guest on May-28-2020
0

delete and return element from set python

>>> s = set([1])
>>> print s.pop()
1
>>> print s
set([])
>>> print s.pop()
KeyError: pop from an empty set
Posted by: Guest on August-04-2020

Code answers related to "delete and return element from set python"

Python Answers by Framework

Browse Popular Code Answers by Language