how to add a new element to a set in python

To add a new element to a set in Python, you can use the add() method. Here's an example:

main.py
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}
71 chars
4 lines

In this example, we created a new set my_set with the elements 1, 2, and 3. Then we used the add() method to add the element 4 to the set. Finally, we printed the updated set, which now includes the new element.

related categories

gistlibby LogSnag