✦ Python Foundations Collections module

Concept 06 · Lists

Keep related values
together in order.

Lists let Python keep several values in one ordered, changeable collection.

01 Learn02 Try03 Predict

Learning as a guest

Learning as a guest? Save your progress!

Create a free account to learn with Sloby AI, check your understanding, and track your progress.

The entire platform is free throughout 2026. Foundations stays free forever.

A list keeps related values together in order.

A variable can refer to one value. A list lets one variable refer to a collection of related values, kept in a defined order. This is useful when your program needs to work with several fruits, names, grades, or products instead of giving every value its own variable.

fruits = ["apple", "banana", "orange"]
print(fruits)

Square brackets [] mark a list, and commas separate its items (also called elements). Here, fruits refers to one list containing three strings. Python keeps their order: apple comes before banana, and banana comes before orange.

['apple', 'banana', 'orange']

Each item has an index: the position Python uses to find it. List indexes start at 0, so the first item is fruits[0], the second is fruits[1], and the third is fruits[2]. Think: fruits → 1 → "banana".

↳

A useful picture: fruits → ["apple", "banana", "orange"]
indexes:           0        1        2. The first position is numbered 0; the index is not the item itself.

Access an item before changing it
fruits = ["apple", "banana", "orange"]

print(fruits[0])
print(fruits[1])

Read list_name[index] as: find the item at this position. fruits[0] prints "apple", and fruits[1] prints "banana". First learn to find an item; then you can change a specific one.

Change an item — lists are mutable
fruits = ["apple", "banana", "orange"]

fruits[1] = "pear"

print(fruits)

fruits[1] names the second item, which was "banana". Assigning "pear" replaces that value at the same position, so the result is ["apple", "pear", "orange"]. Lists are mutable: their contents can change after the list is created; you did not create a new list just by changing one item.

len() counts items, not indexes
fruits = ["apple", "banana", "orange"]

print(len(fruits))

len(fruits) means “how many items are in this list?” and prints 3. A three-item list has indexes 0, 1, and 2: its length is 3, but its last valid index is 2. Length and last index are not the same thing.

append() adds one item at the end
fruits = ["apple", "banana"]

fruits.append("orange")

print(fruits)

append() adds one item to the end of a list. Here the list changes from ["apple", "banana"] to ["apple", "banana", "orange"]. It is a simple first way to grow a list.

A list can hold different value types
profile = ["Ana", 16, True]

Python allows a list to hold values of different types, such as text, a number, and a Boolean value. In many practical examples, though, a list groups values with a similar meaning—for example names, grades, or products—so the list is easier to understand.

Loop through a list one item at a time
fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

Lists and loops work naturally together. The list supplies the values, and for visits them one by one: fruit becomes "apple", then "banana", then "orange". Think: list → next item → loop variable → run block. This is like for number in range(3), except you now move directly through a collection of values.

Trace a list as it changes
colors = ["red", "blue", "green"]

print(colors[0])

colors[1] = "yellow"
colors.append("purple")

print(colors)
print(len(colors))

Before running this code, predict four things: the first printed color, which item changes, where append() puts "purple", and the final length. The first print is "red"; index 1 changes blue to yellow; append adds purple at the end; and the final list has 4 items.

Common beginner mistakes

  • Starting with index 1The first list item is at index 0, not index 1. Index 1 names the second item.
  • Using an index that does not existFor ["apple", "banana", "orange"], valid indexes are 0, 1, and 2. Asking for fruits[3] raises an IndexError because there is no fourth item.
  • Treating len() as the last indexIf a list has length 3, its final index is 2. len() counts items; it does not name the final position.

Python Playground

Loading Python runtime…

Before running the code, predict the list and its length. Change one item, print a different index, then add a fruit with <code>append()</code>. Finally, write a short <code>for</code> loop to print each fruit and compare the output with your prediction.

lists.pyPython
OutputWaiting for Python
The browser runtime is loading. You can still edit the example code.

Pyodide runs Python in a browser Web Worker. It is stopped and restarted if code keeps running. Browser execution is not a complete security boundary; do not enter secrets or rely on it for untrusted code isolation.

What will this program print?

Follow the index that is changed before print() runs.

colors = ["red", "blue", "green"]
colors[0] = "yellow"

print(colors)
Choose the output
Sloby, the Python AI Tutor mascot

Your learning mentor

Sign in to ask Sloby

Create a free account or sign in to get AI guidance while you learn.

Sign in to check your understanding

Create a free account or sign in to receive AI feedback and save your learning progress.

Python AI Tutor · Learn by understanding, not just by running code.