Skip to content
Zeni Study
  • Pages
    • Learn Python In 7 Days
      • Day 1 : Variable + if/else
      • Day 2: Loops
      • Day 3 : Array + String
      • Day 4 — Functions
      • icon picker
        Day 5 — Dictionaries - Hashmap
      • Day 6 — Files & Errors
      • Day 7 — Mini Project & Programmer Thinking
    • Machine Coding:
      • Day 1 — Bank account + Wallet :
      • Day 2 : RateLimiter + seat booking
      • Day 3 : Library Management + Classroom Attendance System
      • Day 4 : Bank account and notification system.
      • Day 5 : Key-value and payment processor.
      • Day 6 : Discount engine + Seat booking( in depth)
      • Day 7: Parking lot System :
    • Machine coding extended:
      • Day 7.5 — Parking Lot (Constraint-Heavy Variant)
      • Day 8 : Order + Payment System
      • Day 8 — Extension Round (Mid-Interview Change)
      • Day 9 — Ticket Booking with Expiry (Simulated Time)
      • Day 9 — Extension Round (Seat Lock Ownership + Forced Unlock)
    • SQL + DB Thinking course
      • Day 1 — Tables, Rows & Thinking in Data
      • Day 2 — Relationships & Foreign Keys (Thinking in Connections)
      • Day 3 — Filtering, Aggregation & Answering Data Questions
      • Day 4 — JOINs (Combining Tables the Right Way)
      • Day 5 — Subqueries, EXISTS & NOT EXISTS (Thinking in Layers)
      • Day 6 — Indexes, Constraints & How Databases Think
      • Day 7 — Full SQL Interview Simulation (SDE-1 Ready)
      • Extended Day 1 — Core Business Systems (Deep Practice)
      • Extended Day 2 — Booking & Platform Systems (High-Depth Practice)
      • Extended Day 3 — Content & Learning Platforms (Capstone Practice)
    • LLD: Java
      • DAY 1 — Core Foundations (SDE-2 Level)
      • DAY 2 — State, Time & Domain Correctness
      • DAY 3 — Extensibility & Failure Handling (SDE-2 Core Signal)
      • DAY 4 — Concurrency, Scheduling & Consistency (SDE-2 Critical)
      • DAY 5 — Workflow Design & Config-Driven Systems (SDE-2+)
      • DAY 6 — Eventing & Usage-Based Systems (Advanced SDE-2)
      • DAY 7 — Interview Simulation (Hard, Production-Grade)
      • Hidden Edge Cases Interviewers Expect (Day-wise)
      • Detailed Testing file:
    • Assignment:
      • If else Practice Set

Day 5 — Dictionaries - Hashmap

🟢 Day 5 — Dictionaries (Key–Value Thinking)

1️⃣ Why Lists Are Not Enough

So far, we used lists:
names = ["Amit", "Rahul", "Neha"]
marks = [85, 90, 78]

Problem ❌

Which mark belongs to which student?
You must remember positions
Data can easily get mismatched
💡 Lists work well when order matters, but fail when meaning matters.

2️⃣ Real-World Analogy

Think of a phone contacts app:
You don’t say: “Give me contact at position 5”
You say: “Give me Rahul’s number”
That is key → value.
🧠 Dictionary = lookup by meaning, not position

3️⃣ What Is a Dictionary?

A dictionary stores data as:
key → value
Example:
marks = {
"Amit": 85,
"Rahul": 90,
"Neha": 78
}
Keys are unique
Values can repeat
Order is not important (for beginners)

4️⃣ Accessing Values

print(marks["Amit"])
🧠 You don’t “search”. You directly look up using key.

5️⃣ What If Key Does Not Exist? (Very Important)

print(marks["Suresh"])
❌ Program crashes.
Why?
Dictionary does not guess
Key must exist

Safer Way

if "Suresh" in marks:
print(marks["Suresh"])
else:
print("Student not found")
🧠 Always check before accessing.

6️⃣ Adding & Updating Values

Add New Key

marks["Suresh"] = 88

Update Existing Key

marks["Amit"] = 90
Same syntax. Python decides whether it’s add or update.

7️⃣ Looping Over Dictionary

Loop Over Keys

for name in marks:
print(name)

Loop Over Values

for score in marks.values():
print(score)

Loop Over Both

for name, score in marks.items():
print(name, score)
🧠 .items() is the most used pattern.

8️⃣ Frequency Counting Pattern (EXTREMELY IMPORTANT)

This pattern appears everywhere:
Word count
Character count
Votes
Logs
Analytics

Example: Count Characters

text = "programming"
freq = {}

for ch in text:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1

print(freq)

How to Think

Check if key exists
If yes → increment
If no → initialize
🧠 This is one of the most important thinking patterns in programming.

9️⃣ Dictionary vs List (Mental Model)

List
Dictionary
Access by index
Access by key
Order-based
Meaning-based
Position matters
Name matters
Slower search
Fast lookup
There are no rows in this table

⚠️ Common Beginner Mistakes

Forgetting to check key existence
Confusing keys and values
Using mutable types as keys
Expecting order
Overusing lists where dict is better

🧪 Practice Questions (Think Before Coding)

Q1

Create a dictionary to store student → marks and print all students with their marks.

Q2

Given a dictionary of items and prices, calculate total cost.

Q3

Count frequency of numbers in a list.
Example:
[1, 2, 1, 3, 2, 1]
→ {1: 3, 2: 2, 3: 1}

Q4

Count frequency of words in a sentence.
Example:
"the cat and the dog and the cat"

Q5

Find the student with highest marks.

Q6

Merge two dictionaries. If key exists in both, add values.

Q7 (Thinking Question)

Why is dictionary better than list for storing marks?
Answer in words.

🔑 HINTS (Read Together, Only If Stuck)

Use if key in dict
Start frequency count from 1
Use .items() to loop key + value
For max: compare values
Use meaningful key names
Think in mapping, not position

✅ What You Should Be Able to Do Now

After Day 5, you should:
Store meaningful data using keys
Look up data safely
Count frequencies
Process real-world structured data
Choose dictionary over list correctly

🧠 Reflection Questions (Answer in Words)

Why do keys need to be unique?
What happens if key does not exist?
When should you prefer dictionary over list?
Why is frequency counting so common?

⭐ Golden Rule from Day 5

If data has meaning, it deserves a dictionary.

 
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.