Adding Set Items in Python: Including Elements in Sets

thumb_up 1  ·  sell Python set item addition, Adding elements to Python sets, Including items in a Python set

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.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back