Tutelage

Lessons › coding and computer science · taught by Leo

Python lists explained: brackets, indexing, and append

Learn how Python lists store values, how square brackets work, why indexing starts at zero, and how append adds items.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app

A list holds many items

A Python list is just a container for many values.

You can keep numbers, words, or a mix.

Think of it like a row of slots you can point to.

Lists use square brackets

A list is written with square brackets and commas between items.

The basic shape is open bracket, items, close bracket.

Commas separate the values.

That is the list syntax Python recognizes.

Predict a list literal

Which is a Python list?

  1. (1, 2, 3)
  2. [1, 2, 3]
  3. {1, 2, 3}
Show answer

[1, 2, 3]: Yes. Square brackets make a list.

Build a list step by step

Let’s build one list and read from it.

Python starts counting positions at zero.

So the first item is at index zero, not one.

That same pattern keeps going for the rest.

index 0

What comes next: indexing

If colors = ['red', 'blue', 'green'], what does colors[1] return?

  1. 'red'
  2. 'blue'
  3. 'green'
Show answer

'blue': Correct. Index 1 means the second item.

Why index zero works

The first item is zero steps from the start, so Python labels it 0.

Imagine the list as slots lined up from left to right.

The first slot is zero steps from the beginning.

That is why the first item gets index zero.

Change a list with append

Lists can grow after you make them.

append puts a new item at the end.

The original items stay in the same order.

append -> end

What does append do?

If scores = [10, 20], what happens after scores.append(30)?

  1. [10, 20, 30]
  2. [30, 10, 20]
  3. [10, 20]
Show answer

[10, 20, 30]: Yes. append adds the new item to the end.

Lists can hold mixed types

A list can store different kinds of values in one place.

Python does not force every item to be the same type.

You can mix strings, numbers, and booleans.

That makes lists very flexible.

Spot a mixed list

Which list has mixed types?

  1. [1, 2, 3]
  2. ['a', 'b', 'c']
  3. [1, 'two', 3]
Show answer

[1, 'two', 3]: Right. That list mixes numbers and a string.

Watch out: off-by-one errors

A very common mistake is starting at one in your head.

Python uses zero-based indexing, so the first item is 0.

That one shift can change the whole answer.

0-based indexing

Your turn: read the list

If nums = [4, 9, 2, 7], what is nums[2]?

  1. 4
  2. 9
  3. 2
Show answer

2: Nice. Index 2 means the third item, which is 2.

List checklist

Here is the quick checklist.

First, use square brackets and commas.

Second, remember indexing starts at zero.

Third, append adds a new item to the end.

Learn this with Leo, freeA voiced lesson with a live whiteboard, quizzes and a tutor you can call, at tutelage.app