← Back to course
Code Crew G4 / Week 02 / Wednesday
3/6
Week 02 Β· Lists

Wednesday

How long is the list?
// Many values, one name
⏱ about 15 min

Wednesday: How long is the list?

The launch schedule keeps growing. Every time a pilot signs up, Raven adds another time to the list. "How many launches are there now?" Rocket asks.

He starts counting with his finger, but the list scrolls off the screen. "Seven? No, wait. Eight?"

Raven smiles. "What do you notice? We keep counting by hand. Could Python count for us?"

Nova lights up her wings. "Would you like a hint? Python has a short command that measures a list. Its name is short for length."

len counts the items

len is short for length. It counts how many items are in a list.

schedule = ["0800", "1100", "1400", "1700", "2000"]
print(len(schedule))
print(schedule[4])
The screen shows
5
2000
  • This list has 5 items, so len gives 5.
  • The indexes go 0, 1, 2, 3, 4.
  • The last index is always one less than len.
  • An empty list [ ] has a length of 0.
tools = ["drill", "tape", "wrench", "rope", "lamp", "clamp"]
print(len(tools))
What number does the program above show?
What is the last index in the tools list?
A list has 4 items.True or false?
len gives 4?
The first index is 1?
Index 3 is the last item?
Index 4 is the last item?
WHY THIS EXERCISEFour items use indexes 0, 1, 2 and 3. There is no index 4.
EMPTY OR NOT?
  • Read the question.
  • Tap your answer.
A list is written as [ ]. What does len give?
Try it
Write a list of five launch times on paper.
Write the len above the list.
Circle the last index.

Practice lab: length and last index

pilots = ["Ada", "Ben", "Cy", "Dee"]
print(len(pilots))
print(pilots[len(pilots) - 1])
What does line 2 show?
What is len(pilots) - 1?
What does line 3 show?
MEASURE THE LIST
  • Read the question.
  • Tap your answer.
What does len([7, 7, 7]) give?
A list has 10 items. What is its last index?
What does len(["a, b, c"]) give?
StatementTrue or false?
len counts the items in a list.?
len([ ]) gives 1.?
A list with 6 items has a last index of 5.?
len gives the last index.?
WHY THIS EXERCISEMixing up length and last index is a very common bug. Now you can spot it.
What does len([2, 4, 6, 8, 10, 12, 14]) give?
WHY THIS EXERCISElen counts how many items there are. The size of each number does not matter.

Super counting! Tomorrow you will find the last item fast and change items in a list.

← Tuesday