Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

Even if a set holds together only immutable objects, set itself is mutable. We can add new items in it with any of the following ways −

add() Method

The add() method in set class adds a new element. If the element is already present in the set, there is no change in the set.

Syntax

set.add(obj)

Parameters

  • obj − an object of any immutable type.

Example

Take a look at the following example −

 
lang1 = {"C", "C++", "Java", "Python"} lang1.add("Golang") print (lang1)

It will produce the following output −

{'Python', 'C', 'Golang', 'C++', 'Java'}

update() Method

The update() method of set class includes the items of the set given as argument. If elements in the other set has one or more items that are already existing, they will not be included.

Syntax

set.update(obj)

Parameters

  • obj − a set or a sequence object (list, tuple, string)

Example

The following example shows how the update() method works −

 
lang1 = {"C", "C++", "Java", "Python"} lang2 = {"PHP", "C#", "Perl"} lang1.update(lang2) print (lang1)

It will produce the following output −

{'Python', 'Java', 'C', 'C#', 'PHP', 'Perl', 'C++'}

Example

The update() method also accepts any sequence object as argument. Here, a tuple is the argument for update() method.

 
lang1 = {"C", "C++", "Java", "Python"} lang2 = ("PHP", "C#", "Perl") lang1.update(lang2) print (lang1)

It will produce the following output −

{'Java', 'Perl', 'Python', 'C++', 'C#', 'C', 'PHP'}

Example

In this example, a set is constructed from a string, and another string is used as argument for update() method.

 
set1 = set("Hello") set1.update("World") print (set1)

It will produce the following output −

{'H', 'r', 'o', 'd', 'W', 'l', 'e'}

union() Method

The union() method of set class also combines the unique items from two sets, but it returns a new set object.

Syntax

set.union(obj)

Parameters

  • obj − a set or a sequence object (list, tuple, string)

Return value

The union() method returns a set object

Example

The following example shows how the union() method works −

 
lang1 = {"C", "C++", "Java", "Python"} lang2 = {"PHP", "C#", "Perl"} lang3 = lang1.union(lang2) print (lang3)

It will produce the following output −

{'C#', 'Java', 'Perl', 'C++', 'PHP', 'Python', 'C'}

Example

If a sequence object is given as argument to union() method, Python automatically converts it to a set first and then performs union.

 
lang1 = {"C", "C++", "Java", "Python"} lang2 = ["PHP", "C#", "Perl"] lang3 = lang1.union(lang2) print (lang3)

It will produce the following output −

{'PHP', 'C#', 'Python', 'C', 'Java', 'C++', 'Perl'}

Example

In this example, a set is constructed from a string, and another string is used as argument for union() method.

 
set1 = set("Hello") set2 = set1.union("World") print (set2)

It will produce the following output −

{'e', 'H', 'r', 'd', 'W', 'o', 'l'}





The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.
Kas see vastus oli kasulik? 1 Kasutajad peavad seda kasulikuks (1 Hääled)