0
0
DSA Pythonprogramming~30 mins

Why Hash Map Exists and What Problem It Solves in DSA Python - See It Work

Choose your learning style9 modes available
Why Hash Map Exists and What Problem It Solves
📖 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.