Python
Module 4: Python CollectionsBeginner20 min read

Python Dictionaries

Key-value pair collections: lookups, updates, comprehension, and iteration.

Key Takeaways & Core Concepts
  • (as of Python 3.7+), mutable collections of key-value pairs. Keys must be unique and of an immutable type (strings, numbers, tuples).
  • Access value: student["name"] or safer student.get("name", "Unknown").
  • Add or update: student["active"] = True.
  • Iterating:

Python Dictionaries

### Python Dictionaries

Dictionaries are ordered (as of Python 3.7+), mutable collections of key-value pairs. Keys must be unique and of an immutable type (strings, numbers, tuples).

PYTHON Code Example
1student = {
2 "id": 101,
3 "name": "Aryan Sharma",
4 "track": "AI & Data Science",
5 "gpa": 3.92
6}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
6 lines

#### Working with Dictionaries:

  • Access value: student["name"] or safer student.get("name", "Unknown").
  • Add or update: student["active"] = True.
  • Iterating:
  • Keys: for k in student.keys():
  • Values: for v in student.values():
  • Both: for k, v in student.items():
Dictionary Iteration and Get
1config = {"host": "localhost", "port": 5000, "debug": True}
2
3for key, value in config.items():
4 print(f"{key.upper()}: {value}")
5
6timeout = config.get("timeout", 30)
7print(f"TIMEOUT: {timeout}s (default)")
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
7 lines
Note: get() returns a fallback default if the key does not exist without raising a KeyError.
Practice & AssessmentPython Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)