Skip to content

Lists — Basics

By the end of this lesson you'll be able to:

  • Create a Python list and access items by index (including negative indexing)
  • Slice a sublist using the [start:end] notation
  • Measure a list's length with len() and test for membership with in
  • Explain the difference between mutable (changeable) and immutable (unchangeable) types

Variables hold one value. But what if you need to store a whole collection of values — like the names of all your friends, or the scores from ten games? That's what lists are for.

Welcome to Chapter 15!

Monty waving welcome Lists are one of the most powerful tools in Python! Once you learn lists, you'll wonder how you ever programmed without them. Let's build our first collection! Let's code it together!

The List Type

A list is an ordered, changeable collection of items. You create one using square brackets, with items separated by commas:

1
2
3
4
colors = ["red", "green", "blue", "yellow"]
scores = [95, 87, 100, 73, 88]
mixed  = ["Monty", 42, True, 3.14]   # lists can hold different types
empty  = []                           # an empty list

Lists can hold any type of value — strings, numbers, booleans, even other lists.


  

List Indexing

Just like strings, each item in a list has an index starting at 0. Use square brackets to access an item: my_list[index].

1
2
colors = ["red", "green", "blue", "yellow"]
           0       1        2        3

What Do You Think Will Happen?

Monty thinking Look at the list colors above. What does colors[2] print? And what about colors[0]? Make your guess — then run it!


  

Were you right? Index 0 is the first item, index 2 is the third.

You can also change an item using its index:

1
colors[1] = "purple"   # replaces "green" with "purple"

Negative Indexing

Negative indexing lets you count backwards from the end. -1 is the last item, -2 is the second-to-last, and so on.

1
2
colors = ["red", "green", "blue", "yellow"]
            -4     -3       -2       -1

This is especially useful when you don't know how long the list is but you always want the last item.


  

List Slicing

Slicing extracts a portion of a list. The syntax is the same as string slicing: my_list[start:end]. It returns a new list containing items from start up to (but not including) end.

1
2
3
4
scores = [95, 87, 100, 73, 88]
print(scores[1:4])   # [87, 100, 73]
print(scores[:3])    # [95, 87, 100]
print(scores[2:])    # [100, 73, 88]

Measuring Length with len()

len() counts how many items are in a list. It works on strings too (counting characters).

1
2
colors = ["red", "green", "blue", "yellow"]
print(len(colors))   # 4

Use len() when you need the number of items — for example, to loop exactly as many times as there are items.

Testing Membership with in

The in operator checks whether a value is in a list. It returns True if the value is found, False if not.


  

not in is the opposite — it returns True when the item is not in the list.

Mutable vs Immutable Types

Mutable means you can change something after you create it. Immutable means once created, it cannot be changed — you have to make a new one instead.

Lists are mutable — you can change any item, add items, or remove items.

Strings are immutable — once you create "hello", you cannot change character [0] to something else.

Type Mutable? Example
list Yes colors[0] = "purple" works
str No word[0] = "P" raises TypeError
int, float No x = 5 then x = 6 creates a new int, doesn't change the old one

  

See It: A List Reshaping Itself

Lists are mutable — they change shape in place, and every change renumbers the indices. In the explorer below, predict first: if you insert a new color at index 1, what happens to the index of every color after it? Then click insert(1, ...) and watch.

Explore the List Index Explorer MicroSim

Try a few append() and pop() clicks and watch both index rows renumber. Then click String Test to see the exact TypeError a string gives when you try the same in-place change — the mutable/immutable divide in one screen.

Learning Check

Your Turn — Find the Index Bug

Monty thinking The program below is supposed to print the last item in the list using negative indexing. But it has an off-by-one bug. Fix the index so it prints "kiwi".


  

Experiments

Try these changes. Predict what will happen first, then run it to check!

  1. Create a list of five of your favorite movies. Print the first, the last (using -1), and the middle one. You'll know it worked when all three print without errors.

  2. Slice your movies list to get just the first two items. You'll know it worked when you see a list with exactly 2 movies.

  3. Use len() to print how many movies are in your list. You'll know it worked when you see 5 (or whatever number you used).

  4. Use in to check if your favorite movie is in the list. Print "Found it!" or "Not there!". You'll know it worked when the correct message appears.

  5. Change the second item in your list to "The Matrix". Print the whole list before and after. You'll know it worked when you see the list change.

List Master!

Monty celebrating You've created lists, used indexing and slicing, measured length, checked membership, and discovered the mutable/immutable difference! Next chapter we'll learn how to add, remove, and sort items — making lists even more powerful. Let's keep coding together!

Take the Chapter Review Quiz

See Annotated References