×

Python Set update() method

Python Set update() method

The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item will be updated in the original set.

Syntax

set.update(set1)

Parameter

set- This parameter represents the main set.

set1- This argument signifies the other set to search for equal items.

Return

None

Example 1

# Python program explaining
# the set.update() method 
#initializing set1
set1 = {"mango", "banana", "apple","tomato"}
print("Set 1:",set1)
# initializing set2
set2 = {"potato", "spinach","Carrot", "apple"}
print("Set 2:",set2)
# updates the current set1, by adding items from set2
set1.update(set2) 
print("After the update() method...")
print("Updated Set 1:",set1)
print("Set 2:",set2) 

Output

Set 1: {'tomato', 'apple', 'banana', 'mango'}
Set 2: {'apple', 'Carrot', 'spinach', 'potato'}
After the update() method... 
Updated Set 1: {'apple', 'tomato', 'potato', 'Carrot', 'mango', 'spinach', 'banana'}
Set 2: {'apple', 'Carrot', 'spinach', 'potato'} 

Example 2

# Python program explaining
# the set.update() method 
# initializing set A
A = {"a","b","c","v"} 
print("Set A:",A)
#initializing B
B = {"a","b","c","h","k","r"}
print("Set B:",B) 
#the common items will have only one appearance in Set A
A.update(B)
print("\nUpdated A:",A)
print("Set B:",B) 

Output

Set A: {'b', 'a', 'v', 'c'}
Set B: {'k', 'h', 'r', 'a', 'b', 'c'}
Updated A: {'k', 'h', 'v', 'r', 'a', 'b', 'c'}
Set B: {'k', 'h', 'r', 'a', 'b', 'c'} 

Related Topics

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

How to reverse a string in python

How to reverse a string in python A Brief About Strings- The String is a data type in Python that has a sequence of characters. This series of characters are represented in...

4 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Assertion Errors and Attribute Errors in Python

Assertion Error in Python In Python, the assert condition is used to continue the execution if the given statement displays true. If the assert statement displays false, it raises Assertion Error...

3 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Kite Python

Kite in Python: The Kite is a package provided by the python programming language; it works with the help of artificial intelligence and helps us write code inside the visual studio....

3 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

How to import pandas in python

How to Install Pandas in Python Python is a vast ocean of libraries, modules, and different functions. It has a solution for almost everything. Using Python, we can simplify even a...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

Python all() Function

Python all() Function The all() function in Python returns a Boolean value ‘True’ if all the items in iterable are true or if the iterable object is empty, else it returns False. Syntax all(iterable) Parameter Iterable: An iterable object...

1 minute read.

Type casting in Python

Introduction Type Casting is defined as the method of converting the variables/values data type into a certain data type for matching the operation needed to be performed by the users. This...

4 minutes read.

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they...

3 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

How to Print Pattern in Python

How to Print Pattern in Python. Pattern programs are really important for beginners and they are always asked in all the technical interviews. Multiple loops approach is used to print the different...

6 minutes read.

Python String Methods

Python String Methods Python has a set of built-in string methods. The Python string methods are as follows: capitalize() The string.capitalize() method returns a copy of the string by converting the...

5 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.