0Pricing
Mojo Academy · Lesson

Key-Value Data with Dict

Look up values by key.

Key-Value Data with Dict is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎯

Frequently asked questions

Is the “Key-Value Data with Dict” lesson free?

Yes — the full text of “Key-Value Data with Dict” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Key-Value Data with Dict”?

Look up values by key. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Key-Value Data with Dict” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Building and Growing a List
  2. Key-Value Data with Dict
  3. Unique Items with Set
  4. Choosing the Right Collection
← Back to Mojo Academy