Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

🟢 Beginner🔵 Advanced
🎯 Why Data Structures Matter📋 Lists: Ordered Collections🔑 Dictionaries: Key-Value Pairs🎪 Sets: Unique Values & Speed📦 Tuples: Immutable Sequences⚖️ Comparing All Data Structures🔧 Mastering List Methods⚡ Advanced Dictionary Patterns🔄 Power of Set Operations🏗️ Building Nested Structures⚙️ Performance Tuning & Optimization
Python/Data Structures/Dictionary Basics

🔑 Dictionary Basics — Key-Value Lookups

Dictionaries store data as key-value pairs, letting you look up values by meaningful names instead of numeric positions.


🎯 Creating Dictionaries

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
}

💡 Accessing Dictionary Values

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)

🎨 Real-World Example: Phonebook

# 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")

📊 Dictionary vs List

AspectListDictionary
AccessBy position (0, 1, 2...)By key name
Example`list[0]``dict["name"]`
Best forOrdered itemsNamed lookups
SpeedMediumVery fast

🔑 Key Takeaways

  • ✅ Dictionaries use `key: value` pairs
  • ✅ Access values with `dict[key]` notation
  • ✅ Keys must be unique (usually strings or numbers)
  • ✅ Use `.get()` to safely access keys
  • ✅ Check if key exists with `in` operator

Ready to practice? Challenges | Quiz


Resources

Python Docs

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

Courses

PythonFastapiReactJSCloud

© 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn