0Pricing
Mojo Academy · Lezione

Dati chiave-valore con Dict

Cerchi i valori tramite chiave.

Dati chiave-valore con Dict è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Look Up by Key

When you want to find a value by a name instead of a position, use a Dict. It maps keys to values. 🔑

Two Types in Brackets

A Dict declares two types: the key type and the value type, like Dict[String, Int].

var ages = Dict[String, Int]()

Add a Pair

Store a value by assigning through its key in brackets. This inserts the key-value pair into the dict.

ages["Ada"] = 36

Read a Value

Fetch a value by putting its key in brackets. The dict returns whatever you stored under that key.

var a = ages["Ada"]

Update in Place

Assigning to an existing key overwrites its value. Keys stay unique, so you never get duplicates.

ages["Ada"] = 37

Check Membership

Test whether a key exists with the in operator. It returns True or False without raising an error.

if "Ada" in ages:
    print("found")

Size of a Dict

Count the pairs with len. It reports how many key-value entries the dict currently holds.

var count = len(ages)

Iterate the Keys

Looping a dict walks its keys. Use each key to reach its value inside the loop body.

for name in ages:
    print(name)

Remove a Pair

Delete an entry with the pop method, passing the key. It removes the pair and returns its value.

var removed = ages.pop("Ada")

Keys Must Be Hashable

A dict finds values fast because keys are hashable. Strings and Ints work great as keys.

Fast Average Lookup

Lookups, inserts, and updates are roughly constant time. That speed is why dicts power so much real code.

Quick Check

A Dict already has the key "Ada". You assign ages["Ada"] = 37. What happens to the dict size?

Recap

A Dict maps unique keys to values for fast lookup. Use brackets to set and get, in to check, and len to count. 🎯

Domande Frequenti

La lezione «Dati chiave-valore con Dict» è gratuita?

Sì — il testo completo di «Dati chiave-valore con Dict» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.

Cosa imparerò in «Dati chiave-valore con Dict»?

Cerchi i valori tramite chiave. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Mojo Academy?

Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Dati chiave-valore con Dict»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Mojo Academy?

Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Costruire e ampliare una List
  2. Dati chiave-valore con Dict
  3. Elementi univoci con Set
  4. Scegliere la collection corretta
← Torna a Mojo Academy