×

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

Cube Root in Python

In general, the cube is a three-dimensional solid figure that has 6 square faces. It is also called a geometrical shape with six equal faces, eight vertices, and twelve edges....

3 minutes read.

Python List

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating...

4 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Arguments and parameters in Python

Generally, there is a misunderstanding that parameters and arguments, both are the same. But, as a programmer, you need to understand the difference between these two. Parameters: While defining a function, we...

5 minutes read.

Is Python Object Oriented Programming language

In this tutorial, we will see whether python also belongs to an object-oriented programming language like several others. We will also see the advantages of using OOPs. Further, we will...

4 minutes read.

Difference between Python and CPP

Difference between Python and C++ We all know that both C++ and Python are two of the most popular programming languages. Both C++ and Python shares some basic similarities such as...

6 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute 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.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.