How to Add Item to Set in Python?

In this Python tutorial, you will learn how to use Python set add items to add elements or items to a set.

Generally, I use the set where I need to store the unique elements like storing usernames in the web application.

So, in this tutorial, I have explained about the set and how to add items to a set using the two methods add() and update().

Add Item to Python Set

I will explain a few approaches to adding an item to a set in Python here, but before that, let me tell you the definition of a set if you don’t know.

The set is used to store unordered collections of elements or items, but it stores unique elements or items; if you try to add duplicate items to the set, it will remain unchanged.

To represent the set in Python, the elements or items are added within left and right curly braces { } like this {3, 4, 5}, this set contains 3 elements or items of type integer. Similarly, you can create any type of set containing different types of elements like

You know about a set and how it is created. Let’s move on to see how to add items to the set.

Python Add Item to Set using add() Function

The add() method of Python is a built-in method that adds items or elements to the set, but if those items already exist in the set, it doesn’t add them to the set.

READ:  ModuleNotFoundError: No module named 'tensorflow.python.keras'

Suppose you have a set containing a username, as shown below.

username = { "Roy933", "Philip", "Vincent", "Bobby"}

the above-set username stores the user’s username whenever the user registers on the website; here, using the set only allows a unique username.

Now consider a new user registers then you are going to add that username to the set, for that you can use the add() method in your code as shown below

username.add("Eugene")

print(username)
Python Set Add Item using add() Method

Look, the set contains a new username, Eugene, which you added using the add() method.

But using the add() method, you can add only single items to the set; what happens if required to add more than one item to the set? Don’t worry; Python has one more method called update(); look in the next section for it.

Add Element to Set Python using update() Function

The update() method of Python allows you to update the set but you can update or add multiple items to the set.

For example, suppose you need to add three more usernames in one go; then, you can use the update() method as shown below.

username = { "Roy933", "Philip", "Vincent", "Bobby"}

username.update({"USAPatriotPrime","USAFervent","AmericanAspirant"})

print(username)
Add Element to Set Python using update() Function

Look at the output; the update() method adds three more “USAPatriotPrime”, “USAFervent”, and “AmericanAspirant”. But if you look closely within the update() method, the three usernames are provided as a separate set like this {“USAPatriotPrime”, “USAFervent”, “AmericanAspirant”}.

Here is a tip for you: using the update(), you are not only limited to the set but can also add any iterable object (list, tuple, dictionaries, etc.) to it.

For example, add a list of numbers to the set using the update() method as shown below.

zipcode = {66520}

list_zipcode = [66053, 64703, 64123]

zipcode.update(list_zipcode)

print(zipcode)
Python Add to Set using Update() Method

When you print the set “zipcode”, it looks like this {66520, 66053, 64703, 64123} on your terminal.

READ:  Horizontal line matplotlib

Also, remember if you have an empty set like data={}, you can add items to this set using the add() if you want to add a single item at a time but can’t add any iterable object using the update() method when you have empty set.

From the above example, you understand how to add the item to a set.

Conclusion

In this Python tutorial, you learned about Python set add items using the add() and update() methods.

You also learned the difference between add() and update() methods and when to use them.

You may like to read: