
Python set pop() - Remove one random element from the set
The pop() of Python set removes one random element from the set and return it.
s = {'a', 'b', 'c'}
p = s.pop()
print(s) # {'b', 'c'}
print(p) # a
a
is popped in the example but the popped element randomly changes.
s = {'a', 'b', 'c'}
p = s.pop()
print(s) # {'a', 'b'}
print(p) # c
remove()
s = {'a', 'b', 'c'}
p = s.remove('b')
print(s) # {'c', 'a'}
print(p) # None
If you want to remove the specified element, remove() is better.
Comments
Powered by Markdown