GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Set update() - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Set update()

Last Updated : 28 Apr, 2025

update() method add multiple elements to a set. It allows you to modify the set in place by adding elements from an iterable (like a list, tuple, dictionary, or another set). The method also ensures that duplicate elements are not added, as sets inherently only contain unique elements. Example:

Python
s = {1, 2, 3}

s.update([3, 4, 5])
print(s)

Output
{1, 2, 3, 4, 5}

Syntax of update()

set.update(*others)

  • *others: One or more iterables (sets, lists, tuples, strings, etc.) from which elements will be added to the set.

Examples of update()

Example 1: We can use update() to add all elements from one set to another. Any duplicate elements will be ignored.

Python
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}

d1.update(d2)
print(d1)

Output
{'a': 1, 'b': 3, 'c': 4}

Explanation: Since 3 is present in both sets, but it only appears once in the updated set1 because sets don’t allow duplicates.

Example 2: We can also pass a list (or any iterable) to update() to add its elements to the set.

Python
d1 = {'a': 1, 'b': 2}
d1.update([('c', 3), ('d', 4)])
print(d1)

Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Explanation: update() method is called on d1 with the iterable [('c', 3), ('d', 4)], which adds the key-value pairs 'c': 3 and 'd': 4 to d1.

Example 3: You can update a set with multiple iterables in a single operation.

Python
d1 = {'a': 1, 'b': 2}
d1.update(c=3, d=4)
print(d1)

Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Explanation: update() adds the key-value pairs 'c': 3 and 'd': 4 to d1, creating new keys with the specified values.

Example 4: update() method can also be used to add individual characters from a string to the set.

Python
s1 = {1, 2, 3}
s1.update("hello")
print(s1)

Output
{1, 2, 3, 'e', 'l', 'h', 'o'}

Explanation: Each character in the string "hello" is treated as a unique element and added to the set. Duplicates (like 'l') are ignored.

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.