Lecture notes · Programming in Python · Lecture 11

Aliasing and Cloning

MIT 6.100L Intro to CS and Programming using Python, lecture 11, 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 shows how Python copies a list, how two names can point to the same list, and how to change a list in place by clearing it and adding items back.

Key ideas

Words to know

mutable
able to be changed after it is made. A list is mutable, so you can add, remove, or change its items.
aliasing
having two or more names that all point to the same object in memory, like two nicknames for one person.
clear
a list function that empties the list completely, removing every item in it.
pop
a list function that removes and gives back the last item in the list when you call it with no input.

Example

Think of a shared notebook. If you photocopy it, you get a second notebook you can write in without changing the first one, which is like making a copy of a list. If two people both call the same notebook by different nicknames, writing in it through either nickname changes the one notebook, which is like aliasing.

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

Part 2 · 10:00–20:00

This part explains three ways to remove items from a Python list, and why changing a list while looping over it can cause mistakes.

Key ideas

Words to know

mutate
to change something in place. When a list is mutated, the same list is changed rather than a new one being made.
return value
the result a function hands back when it finishes, which you can store or use later.
iterate
to go through items one at a time, such as each item in a list, usually with a loop.
index
the position of an item in a list. The first item is at index 0, the next at index 1, and so on.

Example

Imagine a line of people waiting at a ticket counter, and you tell each person who is not allowed in to step out. If you remove someone and keep walking forward without looking back, the person who moved into their spot gets skipped. The same thing happens when you remove items from a list while you loop through it.

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

Part 3 · 20:00–30:00

This part explains why changing a list while you loop over it causes skipped items, and how copies and aliases affect what gets changed.

Key ideas

Words to know

mutate
to change an object in place, such as adding or removing items from a list.
alias
another name that points to the same object, so changing the object through either name changes it for both.
clone
a separate copy of an object. Changing the clone does not change the original.
mutable
able to be changed after it is created. Lists are mutable.

Example

Boston is also called the Hub or Beantown. These are all names for the same city, so if it starts snowing in Boston, it is snowing in the Hub and in Beantown too. An alias works the same way: two list names pointing to one list means one change shows up under both names.

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

Part 4 · 30:00–40:00

This part explains how Python names and copies lists, and why changing one name can change what another name shows.

Key ideas

Words to know

alias
Two different names that point to the same object in memory. Changing the object through one name changes it for both.
mutable
Able to be changed after it is made. Lists are mutable, while numbers and strings are not.
shallow copy
A copy of only the outside (top) level of a list. Any lists or other changeable items inside it are still shared with the original.
deep copy
A copy that also copies the items inside, so the two lists do not share any changeable parts. The lesson names this but does not explain it in this part.

Example

Imagine two people share one notebook, and both write in it. If one person changes a page, the other person sees the change, because they are looking at the same notebook. A shallow copy is like photocopying only the cover of a binder, so the two binders still share the same pages inside.

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

Part 5 · 40:00–46:10

Copying a list can leave the inner parts shared between the two copies, and a deep copy is the way to make fully separate ones.

Key ideas

Words to know

mutable
able to be changed after it is created, like a list you can add items to.
shallow copy
a copy that makes a new outer list but reuses the inner items, so those inner items are still shared with the original.
deep copy
a copy that makes new versions of everything inside as well, so the two lists share nothing.
side effect
an unplanned change that happens to something else when you run an operation on another thing.

Example

Imagine two copies of a family photo album made with a shallow copy: each has its own cover and page list, but the photos inside are the same prints. If you write on a photo in one album, the other album shows the writing too. A deep copy is like making new prints of every photo, so each album is fully separate.

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

← Lecture 10: Lists and Mutability · Lecture 12: List Comprehension, Functions as Objects, Testing, and Debugging →

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