Python Set update()


Python Set update() method

Python Set update() method updates the set with the items from given iterable. The iterable could be another set, a list, a tuple, a string, etc.

In this tutorial, you will learn the syntax and usage of Set update() method, with examples.

Syntax of Set update()

The syntax to call update() method is

set.update(s)

Parameters

Set udpate() method takes one parameter.

ParameterDescription
sRequired
An iterable object. Could be a list, set, tuple, string, dictionary keys, dictionary values, etc.
Python Set update() method parameters table

Return value

update() method returns None.

Please note that the Set update() method modifies the original set.

Usage

Updating from a set

The following statement shows how to update a set set_1 with the items from another set set_1 using set.update() method.

set_1.update(set_2)

This statement is equivalent to the following operation.

set_1 = set_1.union(set_2)

Updating from a list

The following statement shows how to update a set set_1 with the items from a list list_1 using set.update() method.

set_1.update(list_1)

Similarly, you can update the set with items from iterables like a tuple, a string, etc.

Examples

1. Updating set with items from another set in Python

In this example, we shall take two sets in set_1 and set_2. We have to update the set set_1 with the items from the set set_2.

Call update() method on set_1 object, and pass set_2 object as argument.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}

print(f"set_1 before update : {set_1}")

# Update set_1 with items from set_2
set_1.update(set_2)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'d', 'c', 'b', 'a'}
set_1 after  update : {'a', 'c', 'b', 'd', 'm'}

2. Updating set with items from a list in Python

In this example, we shall take a set in set_1 and a list in list_1. We have to update the set set_1 with the items from the list list_1.

Call update() method on set_1 object, and pass list_1 object as argument.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
list_1 = ['a', 'p', 'l', 'e']

print(f"set_1 before update : {set_1}")

# Update set_1 with items from the list
set_1.update(list_1)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'d', 'b', 'c', 'a'}
set_1 after  update : {'d', 'p', 'l', 'c', 'b', 'e', 'a'}

3. Updating set with items from a string in Python

String is a collection of characters.

In this example, we shall take a set in set_1 and a string in str_1. We have to update the set set_1 with the character strings from the string str_1.

Call update() method on set_1 object, and pass str_1 object as argument.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
str_1 = 'apple'

print(f"set_1 before update : {set_1}")

# Update set_1 with items from the string
set_1.update(str_1)

print(f"set_1 after  update : {set_1}")

Output

set_1 before update : {'d', 'b', 'c', 'a'}
set_1 after  update : {'e', 'l', 'p', 'd', 'b', 'c', 'a'}

Summary

In this Python Set Tutorial, we learned how to use Set update() method to update a set with the items from a given iterable in Python, with the help of well detailed examples.