Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
The update() Method | Set
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

book
The update() Method

The update() method in Python allows you to add multiple elements to a set at once. This method takes an iterable (like a list, tuple, or another set) and adds its elements to the existing set.

The update() method can accept:

  • Lists: [movie1, movie2, ...];
  • Tuples: (movie1, movie2, ...);
  • Other Sets: {movie1, movie2, ...}.
12345678
# Original set of favorite movies favorite_movies = {"Inception", "Interstellar", "Tenet"} # Adding multiple new movies favorite_movies.update(["Tenet", "Memento", "The Prestige"]) # Print the updated set print(favorite_movies)
copy

Note

If any element being added already exists in the set, it will not be duplicated. The order of elements in a set is not guaranteed and may vary.

Task
test

Swipe to show code editor

You've watched a few more movies and want to add multiple titles to your favorite_movies set. Use the update() method to add the movies "Memento" and "The Prestige". Print the updated set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
toggle bottom row

book
The update() Method

The update() method in Python allows you to add multiple elements to a set at once. This method takes an iterable (like a list, tuple, or another set) and adds its elements to the existing set.

The update() method can accept:

  • Lists: [movie1, movie2, ...];
  • Tuples: (movie1, movie2, ...);
  • Other Sets: {movie1, movie2, ...}.
12345678
# Original set of favorite movies favorite_movies = {"Inception", "Interstellar", "Tenet"} # Adding multiple new movies favorite_movies.update(["Tenet", "Memento", "The Prestige"]) # Print the updated set print(favorite_movies)
copy

Note

If any element being added already exists in the set, it will not be duplicated. The order of elements in a set is not guaranteed and may vary.

Task
test

Swipe to show code editor

You've watched a few more movies and want to add multiple titles to your favorite_movies set. Use the update() method to add the movies "Memento" and "The Prestige". Print the updated set.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt