Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Aggiungere Elementi a un Dizionario: Aggiornare le Coppie Chiave-Valore | Padroneggiare i Dizionari Python
Strutture Dati Python

book
Aggiungere Elementi a un Dizionario: Aggiornare le Coppie Chiave-Valore

I dizionari sono dinamici, il che significa che puoi aggiungere, aggiornare o rimuovere elementi dopo che il dizionario è stato creato. Esploriamo come aggiungere nuovi elementi a un dizionario.

Inizia creando un dizionario chiamato book con alcuni dettagli iniziali:

book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}
print(book)
12
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} print(book)
copy

Per rendere il nostro dizionario più completo, possiamo aggiungere una nuova coppia chiave-valore. Ad esempio, potremmo voler aggiungere il genre del libro:

book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813}

book["genre"] = "Romance"
print(book)
1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813} book["genre"] = "Romance" print(book)
copy

Dopo aver aggiunto la nuova coppia chiave-valore, il dizionario ora include:

Aggiornare una Coppia Chiave-Valore Esistente

Se hai bisogno di aggiornare il valore di una chiave esistente, puoi farlo riassegnando un nuovo valore a quella chiave. Ad esempio, supponiamo che l'anno di pubblicazione del libro sia stato corretto a 1815:

book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance"}

book["year"] = 1815
print(book)
1234
book = {"title": "Pride and Prejudice", "author": "Jane Austen", "year": 1813, "genre": "Romance"} book["year"] = 1815 print(book)
copy

Nota

Le virgolette singole ' ' e doppie " " in Python sono intercambiabili ed equivalenti.

Compito

Swipe to start coding

Ti viene dato un dizionario authors_books e 2 variabili: author_to_add e fitzgeralds_books.

  • Aggiungi un nuovo autore e la sua lista di libri al dizionario authors_books.
  • L'autore dovrebbe essere la chiave e la lista di libri dovrebbe essere il valore.

Soluzione

authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

author_to_add = 'F. Scott Fitzgerald'
fitzgeralds_books = ['The Great Gatsby', 'Tender Is the Night', 'This Side of Paradise', 'The Beautiful and Damned', 'The Last Tycoon']

# Write your code here
authors_books[author_to_add] = fitzgeralds_books

# Testing
print("Updated dictionary:", authors_books)
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 4
authors_books = {
'William Shakespeare': ['Hamlet', 'Macbeth', 'Romeo and Juliet', 'Othello'],
'J.K. Rowling': ['Harry Potter and the Sorcerer\'s Stone', 'Harry Potter and the Chamber of Secrets', 'Harry Potter and the Prisoner of Azkaban', 'Harry Potter and the Goblet of Fire'],
'George Orwell': ['1984', 'Animal Farm', 'Coming Up for Air'],
'Stephen King': ['It', 'The Shining', 'Carrie', 'Misery'],
'Agatha Christie': ['Murder on the Orient Express', 'The Murder of Roger Ackroyd', 'And Then There Were None', 'Death on the Nile']
}

author_to_add = 'F. Scott Fitzgerald'
fitzgeralds_books = ['The Great Gatsby', 'Tender Is the Night', 'This Side of Paradise', 'The Beautiful and Damned', 'The Last Tycoon']

# Write your code here
___

# Testing
print("Updated dictionary:", authors_books)

Chieda ad AI

expand
ChatGPT

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

some-alt