Python Lists

Let’s look at lists

  1. Notice

    A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3]
other = items
other.append(9)
print(items)
print(other is items)
Live state

Top-level scope

items→Object 1
other→Object 1
Object 1 · list[3, 9]
This step

A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.

Ordered positions

Lists preserve order. Use a zero-based index to access a position.

Image brief

Alt text
A list containing tea and coffee shows tea at index zero and coffee at index one.
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Lists". Teaching focus: Lists preserve order. Use a zero-based index to access a position. Depict exactly the relationships and labels in this reference diagram: items = ["tea", "coffee"] INDEX 0 1 +-------+ +----------+ VALUE | "tea" | | "coffee" | +-------+ +----------+ items[0] --> "tea" Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A list containing tea and coffee shows tea at index zero and coffee at index one.

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3]
other = items
other.append(9)
print(items)
print(other is items)
Live state

Top-level scope

items→Object 1
other→Object 1
Object 1 · list[3, 9]
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[3, 9]
True

What do you expect this program to print?

What does this example display?

items = [3]
other = items
other.append(9)
print(items)
print(other is items)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[3, 9] True

Follow the printed values in order. The output is: [3, 9] True

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

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.

Why does items show the change made through other?

Why does items show the change made through other?
Choose one answer. You can try again.

Answer and explanation

Both names refer to the same list object.

Both names refer to one mutable list. Appending through one name changes that shared list; assignment did not copy it.

Introduction to Lists

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.

What is a List?

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!

Nature of Lists

  • Lists can hold different types of items (strings, numbers, even other lists!).
  • Lists are ordered. This means the position of each item is important.
  • Lists can be changed (we can add, remove, or update items).

Basic List Operations (CRUD)

1. Create List

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"]

2. Read/Access Items in List

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.

3. Update/Change Items in List

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".

4. Delete/Remove Items from List

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.

Adding Items

Use append() to add a new item at the end:

colors = ["red", "green", "blue"]
colors.append("purple")
print(colors)
['red', 'yellow', 'purple']

Checking the Length of a List

Use len() to find how many items are in the list:

colors = ["red", "green", "blue"]
print(len(colors))
3

Summary

  • A list holds multiple items in one place.
  • Items are ordered and can be changed.
  • You can create, read, update, and delete items in a list.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A list is mutable. Two names can refer to the same list, so appending through either name changes the shared object.

Watch out for this idea

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.

    Next lesson: Python Dictionaries →