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 freeWatch 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
- A list is mutable, which means you can change it after you create it. Strings and tuples cannot be changed once made. 1:15
- Writing L[3] = 10 goes into the list and replaces the item at index 3 with 10. The list itself is changed, and no new list is made. 2:40
- The name L is a label that points to the list object in memory. Changing an item through L changes that object, not a copy of it. 5:10
- With a tuple, assigning a new value creates a brand-new object and moves the name to it. The old tuple stays in memory, but nothing points to it anymore. 6:14
- The append function adds an item to the end of a list by changing that same list. It does not need to return anything, because its job is the change it makes. 9:34
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
- Append adds an item to the end of a list. It does not give anything back, so it returns None, which means 'no value'. 10:00
- Do not save the result of append back into a variable. If you write L = L.append(5), L gets set to None and you lose your list. 11:25
- Append works by side effect: it changes the list you call it on, and that change is the whole job. Just run the line by itself, then print the list to see the change. 13:32
- Append adds exactly one item. If that item is a list, the new item is a list inside the list, not a set of separate items. 15:44
- Dot notation is written as object.operation. The object sits to the left of the dot, and the operation to be done on it sits to the right. 19:22
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
- A list operation like append looks like a function attached to a list with a dot, such as L.append(x). It adds the item you give it to the end of that list. 20:00
- To build a list of numbers from 0 up to n, start with an empty list, loop over the numbers, and append each one. Then return the list. 21:41
- Do not name your variable list, because list is already the name of the list type. Pick a name like L or my_list instead. 21:41
- To remove an item from a list, loop through it and keep only the items that are not equal to the one you want to remove. The original list stays the same, and you build a new list. 23:38
- You can turn a string into a list. Using split with a space as the separator gives you a list of the words in the string. 27:52
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
- Join puts the items of a list back together into one string. The text before the dot is the character placed between the items, so joining on an empty string gives ABC and joining on an underscore gives A_B_C. 30:40
- Join only works when every item in the list is a string. If the list holds numbers or other types, you must convert each item to a string first. 31:45
- Split turns a string into a list of pieces. Splitting on spaces and then taking len of the list gives a quick word count, which is much easier than tracking spaces one character at a time. 33:36
- Sort and reverse change the list you call them on. After L.sort(), the original order is gone, and no copy is kept. 35:24
- The sorted function does not change the original list. It returns a new sorted copy, which you must save into a variable, and the original list stays the same. 36:32
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
- The function sorted(L) builds a brand-new list in sorted order and leaves the original L as it was. You then store the result under a name, like A. 40:20
- The functions sort() and reverse() change the list L itself. They do their work as a side effect, and they return None, which is nothing at all. 41:06
- Do not write L = L.sort(). Because sort() returns None, that line sets L to None and loses your list. Just write L.sort() on its own line. 41:31
- Functions like sort() and reverse() still need parentheses, even though they take no inputs in them. The list before the dot acts as the input. 42:39
- To write your own function that changes a list in place, you need the index (the position) of each element, not just the element. Enumerate gives you both at once. 48:25
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
- A function can change a list in place, which is called mutating it. Such a function usually returns None, because the change is the job it does. 50:00
- The squaring example walks through the list one index at a time. Starting from 2, 3, 4, the list becomes 4, 9, 16 by the end of the loop. 50:40
- If you call a mutating function without saving its result, you still get the changed list. If you save the result to a variable, that variable holds None. 52:08
- Looping over range(len(L)) builds a fixed sequence of numbers before the loop starts. The loop then appends 0, 1, 2, 3 to the list, and the loop ends after four passes. 55:08
- Looping directly over the elements gives you each item in turn, but you need your own counter if you want to track the position. The lesson keeps a separate variable I that goes 0, 1, 2, 3. 58:41
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
- If you add an item to the end of a list while looping over that same list, the loop never stops. Each time through, the loop moves one step forward, but the end of the list has moved one step further away. 1:00:00
- A loop over range or len builds a fixed set of values before it starts. A loop over a list goes straight through the list itself, so any change to the list affects the loop. 1:00:41
- extend adds every item from another list to the end of your list, and it changes that list in place. Concatenation with + builds a brand-new list and leaves the original lists unchanged. 1:01:24
- In L = L + L, Python makes a new list and moves the name L to point at it. The loop still holds the old list through its loop variable, so it finishes after four passes. 1:06:31
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
- L.clear() empties a list while keeping the same list object. Its contents change, but it is still the same object in memory. 1:10:21
- The id function gives you a number for an object's place in memory. If the number stays the same after you change a list, you changed the original object and did not create a new one. 1:11:05
- Writing L = [] does not empty the old list. It makes the name L point to a new empty list, and the old list is left in memory with no name attached to it. 1:13:17
- Tuples cannot be changed, so they suit fixed facts like a country's latitude and longitude. Lists can change, so they suit things like a grocery list or a list of students. 1:14:19
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