Lecture notes · Programming in Python · Lecture 10

Lists and Mutability

MIT 6.100L Intro to CS and Programming using Python, lecture 10, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part explains that lists can be changed in place, unlike strings and tuples, and what that means for the computer's memory.

Key ideas

Words to know

mutable
Able to be changed after it is created. Lists are mutable.
immutable
Unable to be changed after it is created. Strings and tuples are immutable.
index
The position number of an item in a list. The first item is at index 0.
side effect
A change a function makes to something outside its own work, such as changing a list. append works this way.

Example

Think of a list as a whiteboard and a tuple as a printed sheet of paper. You can erase and rewrite part of the whiteboard, and it is still the same board. To change the printed sheet, you have to print a new one and hand it over, while the old sheet still exists. This comparison is an illustration of the idea, not something stated in the lesson.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains why the append function changes a list in place and returns None, and how to read the dot notation used to call it.

Key ideas

Words to know

mutation
Changing an object directly, so the same object now holds different contents.
side effect
The change a function makes to something else while it runs. Here, the change is to the list, and the function returns nothing useful.
None
Python's way of saying 'nothing'. Functions like append return None because they change the list instead of handing back a value.
dot notation
A way of writing an action on an object: the object comes first, then a dot, then the action, such as L.append(5).

Example

Think of a shopping list that you write on with a pen. Adding 'milk' changes the paper itself, so you don't need to copy the list onto a new sheet. If you wrote down 'the result of adding milk' and put it in place of your list, you would lose the list.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how to build and change lists in Python, using append, loops, and splitting text into lists.

Key ideas

Words to know

append
To add one item to the end of a list.
parameter
A value you pass into a function so it knows what to work with, such as the number n in make_ordered_list(n).
cast
To change a value from one type into another type, such as turning a string into a list with list(s).
split
To cut a string into pieces wherever a certain character appears, and put those pieces into a list.

Example

Think of a list as a line of people at a bus stop. Append is like a new person joining at the back of the line. When you remove everyone who is named Sam, you make a new line that has all the other people, in the same order, and the original line stays as it was.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows how to turn a list back into a string with join, how split and len help count words, and how sort and reverse change a list directly while sorted makes a new copy.

Key ideas

Words to know

mutate
to change an object in place, so the same object is different afterward.
join
to glue the items of a list together into one string, with a chosen character placed between them.
split
to break a string into a list of smaller pieces, using a chosen character as the cut point.
cast
to convert a value from one type to another, such as turning the number 1 into the string "1".

Example

Think of a paper shopping list. Sort is like rewriting the actual list in alphabetical order, so the old order is erased. Sorted is like making a neatly ordered copy and keeping the original list as it was.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

Some list functions create a new sorted or reversed list, while others change the original list directly, and you need to know which is which.

Key ideas

Words to know

mutate
to change an object directly, so the same object now holds different contents.
side effect
something a function does to the outside world, like changing a list, instead of giving back a value.
index
the position of an item in a list, counting from 0 for the first item.
enumerate
a built-in Python function that gives you both the index and the item each time you go through a loop.

Example

Think of a paper shopping list. Using sorted() is like copying the list onto a fresh sheet and writing the items in order, so your old sheet stays the same. Using sort() is like rewriting over your own list, so the original order is gone for good.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part explains how a function can change a list directly, and how looping over a list while adding to it can surprise you.

Key ideas

Words to know

mutate
to change something in place, so the same object is different afterward, without making a new copy.
range
a tool that gives a loop a fixed run of numbers to go through, like 0, 1, 2, 3 for a list of length 4.
loop variable
the name that holds the current value each time a loop goes around, such as I or e.
index
the position number of an item in a list, starting at 0 for the first item.

Example

Think of a shared spreadsheet where you type a new number into an existing cell. Everyone sees the same sheet change, and no new sheet was made. A mutating list works the same way: you change the one list you already have.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part shows why changing a list while you loop over it can make the loop run forever, and how extend and concatenation (joining lists with +) differ.

Key ideas

Words to know

mutate
To change something in place, so the same object is different afterward.
concatenation
Joining two lists together with + to make a new list that holds all the items from both.
binding
The link between a name, like L, and the object in memory that the name points to.
extend
A list action that adds all the items from another list to the end of your list, changing your list directly.

Example

Think of a line of people at a shop where someone at the back keeps joining the line as the front person is served. The line never gets shorter, so the process never finishes. A loop over a list that keeps growing behaves the same way.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:15:07

This part shows how changing a list in place differs from giving a name a brand-new list, and how the ID function lets you check which one happened.

Key ideas

Words to know

mutating
Changing an object in place, so it is still the same object but its contents are different.
ID
A number Python gives an object that stays the same for as long as that object exists. You can use it to check whether two names point to the same object.
immutable
Cannot be changed after it is created. Tuples are immutable.
binding
The link between a name and the object it points to. Reassigning a name breaks the old link and makes a new one.

Example

Think of a name tag stuck on a box. If you empty the box and put the same tag on it, the box is still the same box, just with nothing inside. If you stick the tag on a different box and leave the first box with no tag, the first box is still there, but it no longer has your name on it.

3 checked practice questions for this part are in the app.

← Lecture 9: Lambda Functions, Tuples, and Lists · Lecture 11: Aliasing and Cloning →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York