Python - Adding Removing Elements
Code Life
By Pierre Marsaa
Adding Elements with add()
Sets can be modified, we can add, remove, or update elements. Let's start easy with the add() method.
The add() method allows you to add a single element to a set. Remember that sets automatically maintain uniqueness, so adding an element that already exists won't create a duplicate.
Basic syntax:
Loading editor...
↗️
Loading editor...
↶
↷
Notice how adding 'water' again didn't create a duplicate. The set still contains only unique elements.
add() vs append()
Unlike lists which use append(), sets use add() to add elements. This is because sets don't have a concept of order or position where elements are 'appended' to.
Verify