Remember
A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.
A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = [3]
other = items
other.append(9)
print(items)
print(other is items)Top-level scope
[3, 9]A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.
(no output)
What do you expect this program to print?
items = ["tea", "coffee"]
INDEX 0 1
+-------+ +----------+
VALUE | "tea" | | "coffee" |
+-------+ +----------+
items[0] --> "tea"Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = [3]
other = items
other.append(9)
print(items)
print(other is items)Top-level scope
[3, 9]The example has finished. Compare the final output with the values and references above.
[3, 9] True
What do you expect this program to print?
items = [3]
other = items
other.append(9)
print(items)
print(other is items)In real life, we often need to keep track of multiple items together. For example, a list of groceries, names of your friends, or favorite movies.
In Python, we use something called a list to store many items in one place.
A list is a collection of items. These items are stored in a specific order, and you can add, change, or remove items whenever you want.
Think of it like a shopping list you can edit!
Lists in Python are defined using square brackets [], and the elements are separated by commas. The order of items matters and can be accessed by index.
colors = ["red", "green", "blue"]
We can access items in a list using their position, which is called an index. Indexing in Python starts from 0, so the first element is at index 0, the second at 1, and so on.
colors = ["red", "green", "blue"]
print(colors[0]) # First item
print(colors[1]) # Second item
print(colors[2]) # Third item
red
green
blue
Here, colors[0] gives us "red", colors[1] gives "green", and colors[2] gives "blue". Trying to access an index that doesn’t exist (like colors[3]) will raise an IndexError.
We can change an item in the list by assigning a new value to a specific index. In this example, colors[1] refers to the second item in the list (indexing starts at 0). The value at that index, which was originally "green", is replaced with "yellow". This operation directly modifies the list in-place without creating a new list.
colors = ["red", "green", "blue"]
colors[1] = "yellow"
print(colors)
['red', 'yellow', 'blue']
We changed the second item ("green") to "yellow".
The del keyword is used to delete an item at a specific index in a list. In the example, del colors[2] removes the item at index 2, which is "blue". After this operation, the list only contains ["red", "green"]. The list size decreases, and all elements after the deleted index shift one position to the left. The syntax is: del list[index]. This operation is permanent and does not return the deleted item.
colors = ["red", "green", "blue"]
del colors[2]
print(colors)
['red', 'yellow']
We deleted the third item ("blue"). Now only two colors are left.
Use append() to add a new item at the end:
colors = ["red", "green", "blue"]
colors.append("purple")
print(colors)
['red', 'yellow', 'purple']
Use len() to find how many items are in the list:
colors = ["red", "green", "blue"]
print(len(colors))
3
Connect what you noticed in the example with the idea below.
A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.
Assigning a list to another name copies its elements into a new list.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.