BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set update() method with examples

Last Updated: April 1, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The Set update() method accepts another iterable such as Set, List, String, Dictionary as a parameter and adds the elements of these iterable to the calling set. This method converts the passed iterable to the set before adding their elements to the calling Set. For example – Lets say we have a Set A: {1, 2, 3} and a List lis: [2, “hello”] then calling A.update(lis) would update the set A and the elements of set A after update would be {1, 2, 3, “hello”}.

Set update() method Syntax

set.update(iterable)

Parameters: This method accepts iterable (list, tuple, dictionary etc.) as parameters.
Return Value: It does not return anything, it just updates the calling Set.

Python Set update() method example

In the following example we have two Sets of numbers X & Y and we are calling X.update(Y) to add the elements of set Y to the Set X.

# Set X
X = {1, 2, 3}

# Set Y
Y = {2, 3, 4}

# Displaying X & Y before update()
print("X is:",X)
print("Y is:",Y)

# Calling update() method
X.update(Y)

# Displaying X & Y after update()
print("X is:",X)
print("Y is:",Y)

Output:
Python update() method example

Set update() method example with List, Tuple, String, Dictionary

In the following example we are adding the elements of a list, tuple, string & dictionary to the calling set X using the update() method.

# Set X
X = {1, 2, 3}

# List lis
lis = [3, 5]

# tuple
t = (77, 99)

# String
s = "abc"

# Dictionary dict
dict = {"one": 1, "two": 2}

# Calling update() method
X.update(lis, t, s, dict)

# Displaying X after update()
print("X is:",X)

Output:
Python update() method example with list, tuple, dictionary and string

Top Related Articles:

  1. Python Variables with examples
  2. Python Keywords and Identifiers with examples
  3. Python Set intersection_update() method with examples
  4. Python all() Function with examples
  5. Python abs() Function with examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap