Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

dictionaries-sets

Dictionaries and sets

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Dictionaries and sets: the syntax, patterns, and safety habits you need before advancing in Python.

Lists and dicts are the core data model—know copying, hashing, and when to use sets.

You will apply Dictionaries and sets in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Dicts map hashable keys to values—like JavaScript objects or Java HashMap. Sets hold unique unordered elements—great for membership tests and deduplication.

Dict operations

user = {"name": "Ada", "role": "dev"}
user["email"] = "ada@example.com"
print(user.get("missing", "N/A"))
for key, value in user.items():
    print(key, value)

Sets

tags = {"python", "web", "python"}
print(tags)  # {'python', 'web'}
print("python" in tags)

Keys must be hashable—lists cannot be keys; tuples can if elements are hashable.

Important interview questions and answers

  1. Q: dict[key] vs get?
    A: dict[key] raises KeyError if missing; get returns None or a default.
  2. Q: Set vs list for uniqueness?
    A: Sets enforce uniqueness and O(1) average membership; lists allow duplicates and preserve order.

Self-check

  1. How iterate key-value pairs in a dict?
  2. Can a list be a dict key?

Pitfall: d[key] raises KeyError—use .get() when missing keys are expected.

Interview prep

dict vs list lookup?

Dict average O(1) key lookup; list scan is O(n)—pick dict for keyed access.

Set use case?

Unique membership tests and deduplication—unordered collection of hashable items.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • dict key rules?
  • set use case?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump