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 within - 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!
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 | |
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 | |
What Do You Think Will Happen?
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 | |
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 | |
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 | |
Measuring Length with len()¶
len() counts how many items are in a list. It works on strings too (counting characters).
1 2 | |
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
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!
-
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. -
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.
-
Use
len()to print how many movies are in your list. You'll know it worked when you see5(or whatever number you used). -
Use
into 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. -
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!
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!