← VCE Software Development U4O1

Storing objects in a list

Adds an object to the end of a list so many objects are stored together. You can then loop over the list with a for loop to call each object's methods or find one by its attribute.

Understanding Storing objects in a list in VCE Software Development U4O1

What it does: Empty square brackets make a list with nothing in it yet, ready for append() to add objects. Inside a loop, comparing an attribute such as p.name to a value lets you pick out the matching object. Python lists use the append() method to add an item to the end. There is no add() method on a list. The loop variable (pet) holds one object at a time, so you call the method on pet. A list has no show() method. Use == to compare values. A single = is assignment and causes a syntax error in an if statement. [] creates an empty list. Student() would create one Student object (and needs the right arguments), not a list.

Common mistakes: A tuple () cannot be added to, so append() would fail. Create an empty list with pets = []. A for loop header must end with a colon. It should be for d in dogs:.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 3–79) and word search (Modules 3–114).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line adds a Student object called s1 to a list named students?
students.add(s1)students.append(s1)
Matches
pets.append(Pet("Rex")
↓
The closing bracket for append() is missing, so there is a syntax error. It needs two closing brackets at the end.
Applying
Meanings
Term
objects.append(obj)
Meaning
Adds an object to the end of a list so many objects are stored together. You can then loop over the list with a for loop to call each object's methods or find one by its attribute.
Practice
class Student:
Testing
Gaps
students.(Student("Ava", 7))
Check Answer
Spots
1pets = ()
2pets.append(Pet("Rex"))

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Penguin' badge — List Waddler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line adds a Student object called s1 to a list named students?
A. students.append(s1) ✓
B. students.add(s1)
Python lists use the append() method to add an item to the end. There is no add() method on a list.
2. Which loop correctly calls the show() method on every object in the list pets?
A. for pet in pets: pet.show() ✓
B. for pet in pets: pets.show()
The loop variable (pet) holds one object at a time, so you call the method on pet. A list has no show() method.
3. True or false: A single list can store many objects that were created from the same class.
A. True ✓
B. False
A list can hold any number of objects, such as several Student objects. Looping over it lets you work with each one.
4. Which code creates a Car object and stores it in the list cars in one step?
A. cars = Car("Mazda", 2019).append()
B. cars.append(Car("Mazda", 2019)) ✓
Call append() on the list and pass the new object in as the argument. Objects do not have an append() method unless you write one.
5. You want to find the Student whose name is "Mia" in the list students. Which test inside the loop is correct?
A. if s.name = "Mia":
B. if s.name == "Mia": ✓
Use == to compare values. A single = is assignment and causes a syntax error in an if statement.

Match the Pairs

pets.append(Pet("Rex")) → Creates a new Pet object and adds it to the end of the pets list
for p in pets: p.speak() → Loops over every object in the pets list and calls its speak() method
Create an empty list ready to store many objects → pets = []
Find an object in the list by checking one of its attributes → if p.name == "Rex":
pets.append(Pet("Rex") → The closing bracket for append() is missing, so there is a syntax error. It needs two closing brackets at the end.
for p in pets: speak() → The method is called without an object and is not indented inside the loop. It should be indented and written as p.speak().

Key Term

objects.append(obj)
Adds an object to the end of a list so many objects are stored together. You can then loop over the list with a for loop to call each object's methods or find one by its attribute.

Fill in the Blanks

1. students = [] students.append(Student("Ava", 7))
Hint: Type the list method that adds an object to the end of the list
2. for s in students: s.show()
Hint: Type the keyword that starts a loop over every object in the list
3. for pet in pets: pet.speak()
Hint: Type the loop variable that holds the current object so its method can be called
4. for s in students: if s.name == "Ava": print(s.points)
Hint: Type the operator that checks whether two values are equal
5. students = [] students.append(Student("Liam", 8)) print(len(students))
Hint: Type the built-in function that counts how many objects are in the list

Spot the Error

Question 1
A pets = ()
B pets.append(Pet("Rex"))
C for p in pets:
D p.speak()
Line A — A tuple () cannot be added to, so append() would fail. Create an empty list with pets = [].
Question 2
A dogs = []
B dogs.append(Dog("Rex"))
C dogs.append(Dog("Bo"))
D for d in dogs
E d.speak()
Line D — A for loop header must end with a colon. It should be for d in dogs:
Question 3
A cars = []
B cars.append(Car("Red"))
C for c in Car:
D c.drive()
Line C — The loop must go over the list of objects, not the class name. It should be for c in cars:
Question 4
A players = []
B players.add(Player("Ana"))
C for p in players:
D print(p.name)
Line B — Lists do not have an add() method. Use players.append(...) to store the object in the list.
Question 5
A scores = []
B for n in [5, 8, 9]:
C scores.append(Score)
D for s in scores:
E s.show()
Line C — This appends the class itself, not an object. Create an object with Score(n) so show() works later.

Student with a class code? Join your class →