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 freeWatch 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
- Copying a list makes a brand-new list object in memory with the same elements. You can then change one list without changing the other. 1:35
- Aliasing means giving another name to the same object in memory. Two names for one list are two ways to reach the same list, so a change through either name shows up in both. 0:11
- A function that mutates its input changes the list that was passed in, so it does not need to return anything. To do this for remove_all, first copy the list, then clear the original, then add back only the items that are not equal to the value. 3:00
- The clear function empties a list completely and takes no parameter. It removes every element, not just one. 5:43
- The del statement removes an item at a given index, and pop removes the last item when you call it with nothing in the parentheses. 9:12
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
- The pop method removes the last item from a list and gives that item back, so you can save it in a variable if you want it. 10:19
- The remove method looks for a value and deletes only the first matching item it finds, leaving any other matches in the list. 11:02
- The del keyword deletes the item at a given index, which is its position in the list, and it does not return anything. 12:47
- To remove every copy of a value, you can use a while loop that keeps calling remove as long as the value is still in the list. 15:41
- Removing items inside a for loop that walks over the same list can skip items, because the items shift left but the loop's position does not move back. 18:42
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
- Removing items from a list while a loop walks through that same list can make the loop skip items. When one item is removed, the rest shift over, but the loop moves on anyway. 22:11
- To fix this, loop over a copy of the list and remove items from the original. The copy never changes, so the loop does not skip anything. 24:42
- A copy is a new, separate list with the same items. You make one by writing the list name followed by square brackets and a colon, like L1[:]. 25:04
- An alias is a second name for the same list. If you write just an equals sign between names, Python does not make a copy, so both names point to one list. 28:00
- Because aliases point to the same object, changing the list through one name changes it for every name. So a copy made without the brackets is not really a copy. 28:42
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
- Formal parameters are the placeholder names written in a function definition, and actual parameters are the real values passed in during a function call. 30:45
- When a mutable object, such as a list, is passed into a function, the formal parameter becomes an alias, which is a second name for that same object. Changes made through the formal name also change the original. 31:54
- Assigning one list to a new name, like new_list = old_list, creates an alias. Both names point to the same list in memory, so a change through either name shows up through both. 35:28
- A shallow copy makes a new outer list, but the lists inside it are not copied. The two outer lists therefore share the same inner lists. 37:14
- Adding a new item to the top level of old_list does not change new_list. But changing an inner list through old_list does change new_list, because that inner list is shared. 39:24
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
- A shallow copy, made with copy.copy, copies only the top level of a list. If an item inside is itself mutable (can be changed in place), the copy points to that same item instead of getting its own. 41:04
- Because the inner items are shared, changing one through either list changes it for both. But adding a new item to the top level of one list does not affect the other list. 42:10
- A deep copy, made with copy.deepcopy, makes a new copy of every object at every level. After that, changes to one list never reach the other. 43:14
- Think of objects as things stored in memory and names as labels that point to them. Keeping that picture clear helps you see what an alias (two names for one object) and a clone (a separate copy) really are. 44:43
- Every operation can have a side effect, which is a change that happens beyond what you meant to do. Use tuples for data that should never change, and lists when you want to change data without making new copies each time. 45:05
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