python set remove multiple elements 1

python set remove multiple elements

>>> s = {1, 2, 3, 4, 5}
>>> s.difference_update({1, 2, 3})
>>> s
{4, 5}
>>> s -= {4, 5}
>>> s
set()

Here is what the above code is Doing:
1. We create a set s with the elements 1, 2, 3, 4, and 5.
2. We call the difference_update() method on s, passing in the set {1, 2, 3}.
3. The difference_update() method removes all elements in s that are also in {1, 2, 3}.
4. We print s, which now contains the elements 4 and 5.
5. We call the -= operator on s, passing in the set {4, 5}.
6. The -= operator removes all elements in s that are also in {4, 5}.
7. We print s, which is now empty.

Similar Posts