
Python
Learning Level
Dictionaries store data as key-value pairs, letting you look up values by meaningful names instead of numeric positions.
A dictionary uses curly braces `{}` and stores items as `key: value` pairs.
# Empty dictionary
empty_dict = {}
# Dictionary of student grades
grades = {"Alice": 95, "Bob": 87, "Carol": 92}
# Dictionary with mixed value types
person = {
"name": "Alice",
"age": 25,
"city": "New York",
"active": True
}Access values using the key (not an index number).
student = {"name": "Alice", "grade": "A", "score": 95}
print(student["name"]) # Alice
print(student["score"]) # 95
# Using get() - safer, returns None if key doesn't exist
print(student.get("age")) # None
print(student.get("age", "unknown")) # unknown (default value)# Store phone numbers by name
phonebook = {
"Alice": "555-1234",
"Bob": "555-5678",
"Carol": "555-9012"
}
# Look up a phone number
alice_phone = phonebook["Alice"]
print(f"Alice's number: {alice_phone}") # Alice's number: 555-1234
# Check if a key exists
if "David" in phonebook:
print(phonebook["David"])
else:
print("David not in phonebook")| Aspect | List | Dictionary |
|---|---|---|
| Access | By position (0, 1, 2...) | By key name |
| Example | `list[0]` | `dict["name"]` |
| Best for | Ordered items | Named lookups |
| Speed | Medium | Very fast |
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward