Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Using the update() Method: Merging Multiple Elements into a Set | Mastering Python Sets
Python Data Structures
course content

Kursusindhold

Python Data Structures

Python Data Structures

2. Mastering Python Dictionaries
3. Mastering Python Tuples
4. Mastering Python Sets

book
Using the update() Method: Merging Multiple Elements into a Set

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.

Opgave

Swipe to start coding

You are given the set marvel_movies and the tuple movies_to_add.

  • Add both movies from the tuple to the set.
  • Use the update() method to accomplish this.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
toggle bottom row

book
Using the update() Method: Merging Multiple Elements into a Set

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.

Opgave

Swipe to start coding

You are given the set marvel_movies and the tuple movies_to_add.

  • Add both movies from the tuple to the set.
  • Use the update() method to accomplish this.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt