› Forums › Python › How Python Stores Objects in Dictionaries: A Beginner’s Guide (With Student Example)
- This topic is empty.
-
AuthorPosts
-
February 16, 2026 at 1:03 am #6073
🔹 Initial Context
In many Python programs, especially when learning Object-Oriented Programming (OOP), we often see code like this:
students = {} students["Alice"] = Student(12) students["Bob"] = Student(7)For many learners, this raises an important question:
“Is
Student(12)stored like an array inside the dictionary?”
“How does the object know it belongs to Alice?”This tutorial explains what really happens behind the scenes.
🔹 Step 1: The
StudentClassFirst, consider this simple class:
class Student: def __init__(self, exercises): self.exercises = exercisesThis class represents one student’s exercise record.
Each
Studentobject stores:- One attribute:
exercises - The number of completed exercises
🔹 Step 2: Creating Student Objects
Now look at this line:
Student(12)When Python runs this:
- A new object is created in memory
self.exercisesis set to12- A reference to this object is returned
Think of it as:
A new Student object exists somewhere in memory It contains: exercises = 12The object is not stored in a variable yet. It only exists in memory.
🔹 Step 3: Storing Objects in a Dictionary
Now look at this code:
students = {} students["Alice"] = Student(12)This means:
“Create a Student object and store its reference under the key ‘Alice’.”
Python does NOT copy the whole object into the dictionary.
Instead, it stores a reference (memory address).
Conceptually:
students = { "Alice" → reference to Student(exercises=12) }The dictionary maps the name to the object’s location.
🔹 Step 4: Adding More Entries
When you write:
students["Bob"] = Student(7)Another object is created:
students = { "Alice" → Student(exercises=12), "Bob" → Student(exercises=7) }Each key points to a different object.
🔹 Step 5: Accessing Stored Objects
When you access:
students["Alice"]Python:
- Looks up “Alice” in the dictionary
- Finds the reference
- Returns the object
So:
print(students["Alice"].exercises)Outputs:
12Because you are reading the attribute from the actual object.
🔹 Step 6: Objects Do NOT Store the Key
Important point:
The
Studentobject does NOT know that it belongs to “Alice”.It only knows:
self.exercises = 12The dictionary manages the relationship.
This is good design. It avoids duplicate data.
🔹 Step 7: References and Shared Objects
Consider this example:
a = Student(10) b = a b.exercises = 50 print(a.exercises)Output:
50Why?
Because
aandbpoint to the same object.The dictionary works the same way.
🔹 Step 8: Common Misunderstanding
Many beginners think:
“Is
Student(7)likeStudent[7]?”No.
Syntax Meaning Student(7)Creates an object Student[7]Indexing (not used here) They are completely different.
🔹 Step 9: Visual Memory Model
Think of memory like this:
Objects in Memory
Object A → Student(exercises=12) Object B → Student(exercises=7)Dictionary
"Alice" → Object A "Bob" → Object BThe dictionary stores links, not copies.
🔹 Step 10: Why This Design Is Used in OOP
This design allows:
✅ Clean separation of data and lookup
✅ Easy updates
✅ No duplication
✅ Scalable programsIt is used in:
- Phone books
- Student databases
- Contact apps
- CRMs
- User management systems
🔹 Step 11: Mini Experiment for Learners
Try this code:
class Student: def __init__(self, exercises): self.exercises = exercises students = {} students["Alice"] = Student(12) x = students["Alice"] x.exercises = 99 print(students["Alice"].exercises)Expected output:
99This proves that both variables refer to the same object.
✅ Final Takeaway
In Python:
Dictionaries store references to objects, not the objects themselves.
So:
students["Alice"] = Student(12)Means:
"Alice" → reference → Student objectNot:
"Alice" → copy of objectUnderstanding this is a key step toward mastering object-oriented programming.
- One attribute:
-
AuthorPosts
- You must be logged in to reply to this topic.

