Just like a List, a Set is a type and is, therefore, an object. This means that sets have particular properties and particular methods that they can perform. Let’s look at some of them below.

Adding a single item to a set

We can add a single element to an already existing set using the add method. The only condition is that the item you add must be of the same type as the other items of the set.

When called on a set, the add method has a single parameter which is the item you want to add to a set. The type of the parameter depends on the set you call the method on.

The general syntax is as follows:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy