📖 Scenario: Imagine you have a big box of keys and you want to find the right key quickly. Searching one by one takes too long. A hash map helps you find the right key fast, like having a special map that tells you exactly where each key is.
🎯 Goal: You will create a simple phone book using a dictionary to see how a hash map helps find phone numbers quickly by name.
📋 What You'll Learn
Create a dictionary called phone_book with exact entries for names and phone numbers
Create a variable called search_name with the exact value to look up
Use a for loop with variables name and number to find the phone number for search_name
Print the phone number found or a message if the name is not in the phone book
💡 Why This Matters
🌍 Real World
Hash maps are used in phone books, contact lists, and many apps to find information quickly by a key like a name or ID.
💼 Career
Understanding hash maps helps in software development, especially when working with databases, caching, and fast data retrieval.
Progress0 / 4 steps
1
Create the phone book dictionary
Create a dictionary called phone_book with these exact entries: 'Alice': '555-1234', 'Bob': '555-5678', 'Charlie': '555-8765'
DSA Python
Hint
Use curly braces {} to create the dictionary with keys as names and values as phone numbers.
2
Set the name to search
Create a variable called search_name and set it to the string 'Bob'
DSA Python
Hint
Assign the string 'Bob' to the variable search_name.
3
Find the phone number using a loop
Use a for loop with variables name and number to iterate over phone_book.items(). Inside the loop, check if name equals search_name. If yes, save number to a variable called found_number and break the loop. If no match is found, set found_number to None before the loop.
DSA Python
Hint
Start with found_number = None. Then loop over phone_book.items(). Use if name == search_name to find the number and break the loop.
4
Print the result
Use an if statement to check if found_number is not None. If it is not None, print found_number. Otherwise, print "Name not found".
DSA Python
Hint
Use if found_number is not None to decide what to print.
Practice
(1/5)
1. Why do we use a hash map instead of a simple list to store key-value pairs?
easy
A. To quickly find values using keys without searching the entire list
B. Because lists cannot store multiple values
C. To keep data sorted automatically
D. Because hash maps use less memory than lists
Solution
Step 1: Understand list search time
Searching for a value by key in a list requires checking each item, which takes longer as the list grows.
Step 2: Understand hash map lookup
A hash map uses a key to directly find the value quickly without checking all items.
Final Answer:
To quickly find values using keys without searching the entire list -> Option A
Quick Check:
Hash map lookup is fast key-based access [OK]
Hint: Hash maps speed up key lookups compared to lists [OK]
Common Mistakes:
Thinking lists cannot store multiple values
Believing hash maps keep data sorted
Assuming hash maps always use less memory
2. Which of the following is the correct way to create an empty hash map (dictionary) in Python?
easy
A. hash_map = set()
B. hash_map = []
C. hash_map = ()
D. hash_map = {}
Solution
Step 1: Recall Python data types
Lists use [], tuples use (), sets use set(), and dictionaries (hash maps) use {}.
Step 2: Identify dictionary syntax
Empty dictionary is created with curly braces {}.
Final Answer:
hash_map = {} -> Option D
Quick Check:
Empty dict uses curly braces {} [OK]
Hint: Use curly braces {} to create empty dictionary [OK]