Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Utilizzare il Metodo update(): Unire Più Elementi in un Set | Padroneggiare i Set in Python
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Strutture Dati Python

bookUtilizzare il Metodo update(): Unire Più Elementi in un Set

Il metodo update() in Python ti permette di aggiungere più elementi a un set in una sola volta. Questo metodo accetta un iterabile (come una lista, una tupla o un altro set) e aggiunge i suoi elementi al set esistente.

Il metodo update() può accettare:

  • Liste: [movie1, movie2, ...];
  • Tuple: (movie1, movie2, ...);
  • Altri Set: {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

Nota

Se un elemento che viene aggiunto esiste già nel set, non verrà duplicato. L'ordine degli elementi in un set non è garantito e può variare.

Compito

Swipe to start coding

Ti viene dato l'insieme marvel_movies e la tupla movies_to_add.

  • Aggiungi entrambi i film dalla tupla all'insieme.
  • Usa il metodo update() per realizzare questo.

Soluzione

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 3
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain what happens if there are duplicate elements when using the update() method?

What other set methods are similar to update() in Python?

Can you show how to use update() with a tuple or another set?

close

bookUtilizzare il Metodo update(): Unire Più Elementi in un Set

Scorri per mostrare il menu

Il metodo update() in Python ti permette di aggiungere più elementi a un set in una sola volta. Questo metodo accetta un iterabile (come una lista, una tupla o un altro set) e aggiunge i suoi elementi al set esistente.

Il metodo update() può accettare:

  • Liste: [movie1, movie2, ...];
  • Tuple: (movie1, movie2, ...);
  • Altri Set: {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

Nota

Se un elemento che viene aggiunto esiste già nel set, non verrà duplicato. L'ordine degli elementi in un set non è garantito e può variare.

Compito

Swipe to start coding

Ti viene dato l'insieme marvel_movies e la tupla movies_to_add.

  • Aggiungi entrambi i film dalla tupla all'insieme.
  • Usa il metodo update() per realizzare questo.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 4. Capitolo 3
single

single

some-alt