Home » Python

Python Set update() Method with Example

Python Set update() Method: Here, we are going to learn how to update current set with another set in Python?
Submitted by IncludeHelp, on December 01, 2019

Set update() Method

update() method is used to update this set (set1) by adding the more elements of another set (set2), the method is called with this set (set1) and another set (set2) can be supplied as an argument, method is updated with the elements of another set (set2), if any element exists in this set (set1) then that element will not be added.

Syntax:

    set1.update(set2)

Parameter(s):

  • set2 – It represents another set whose elements to be added in set1.

Return value:

The return type of this method is <class 'NoneType'>, it returns nothing.

Example 1:

# Python Set update() Method with Example

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}

# printing the set before update() call
print("Printing the sets before update() call...")
print("cars_1: ", cars_1)
print("cars_2: ", cars_2)

# updating set (cars_1) by adding 
# the elements of cars_2 elements
cars_1.update(cars_2)

# printing the set after update() call
print("Printing the sets after update() call...")
print("cars_1: ", cars_1)
print("cars_2: ", cars_2)

Output

Printing the sets before update() call...
cars_1:  {'Porsche', 'Lexus', 'Audi'}
cars_2:  {'Porsche', 'Mazda', 'Lincoln'}
Printing the sets after update() call...
cars_1:  {'Porsche', 'Audi', 'Mazda', 'Lincoln', 'Lexus'}
cars_2:  {'Porsche', 'Mazda', 'Lincoln'}

Example 2:

# Python Set update() Method with Example

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "ABC"}

# printing the sets before update() call
print("Before calling update()...")
print("x:", x)
print("y:", y)
print("z:", z)

# Updating the set with other set's elements
x.update(y)
y.update(z)
z.update(x)

# printing the sets after update() call
print("After calling update()...")
print("x:", x)
print("y:", y)
print("z:", z)

Output

Before calling update()...
x: {'XYZ', 'ABC', 'PQR'}
y: {'XYZ', 'ABC', 'PQR'}
z: {'MNO', 'ABC', 'DEF'}
After calling update()...
x: {'ABC', 'PQR', 'XYZ'}
y: {'ABC', 'PQR', 'XYZ', 'MNO', 'DEF'}
z: {'ABC', 'PQR', 'XYZ', 'MNO', 'DEF'}






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.