Aral Code

Collections Lesson 14 of 23

Python Lists

Store many values in a list — create, index and slice lists, change items, add and remove with append, insert, remove and pop, and check length and membership.

A list stores many values in one variable, in order. A class list, a shopping list, the scores of a quiz: all of these fit naturally into a Python list.

Creating a list

students = ["Ana", "Ben", "Carla", "Dan"]
grades = [88, 92, 79, 95]
mixed = ["Ana", 15, True]     # allowed, but usually avoid
empty = []
print(students)
print(len(students))

Output

['Ana', 'Ben', 'Carla', 'Dan']
4

Indexing and slicing

Lists use the same indexing as strings: the first item is at 0, the last at -1.

students = ["Ana", "Ben", "Carla", "Dan", "Ela"]
print(students[0])
print(students[-1])
print(students[1:3])
print(students[:2])

Output

Ana
Ela
['Ben', 'Carla']
['Ana', 'Ben']
print(students[10])

Error

IndexError: list index out of range

Changing items

Unlike strings, lists are mutable: you can change them.

grades = [88, 92, 79]
grades[2] = 81
print(grades)

Output

[88, 92, 81]

Adding items

cart = ["bigas", "itlog"]
cart.append("kape")           # add to the end
cart.insert(0, "pandesal")    # add at position 0
cart.extend(["asukal", "gatas"])   # add several
print(cart)

Output

['pandesal', 'bigas', 'itlog', 'kape', 'asukal', 'gatas']

Removing items

cart = ["pandesal", "bigas", "itlog", "kape", "asukal"]
cart.remove("itlog")          # remove by value
last = cart.pop()             # remove and return the last item
first = cart.pop(0)           # remove and return item at index 0
del cart[0]                   # delete by index
print(cart, last, first)

Output

['kape'] asukal pandesal

Checking and counting

votes = ["Ana", "Ben", "Ana", "Carla", "Ana", "Ben"]
print("Ana" in votes)
print(votes.count("Ana"))
print(votes.index("Carla"))    # position of the first match

Output

True
3
3

Looping through a list

students = ["Ana", "Ben", "Carla"]
for i, name in enumerate(students, start=1):
    print(f"{i}. {name}")

Output

1. Ana
2. Ben
3. Carla

Building a list from input

grades = []
for i in range(1, 4):
    g = float(input(f"Grade {i}: "))
    grades.append(g)

print("Grades:", grades)
print("Average:", round(sum(grades) / len(grades), 2))
print("Highest:", max(grades))

Output

Grade 1: 88
Grade 2: 91.5
Grade 3: 85
Grades: [88.0, 91.5, 85.0]
Average: 88.17
Highest: 91.5

Starting with an empty list and calling append in a loop is one of the most common patterns in Python.

Copying a list

a = [1, 2, 3]
b = a          # NOT a copy: both names point to the same list
b.append(4)
print(a)

c = a.copy()   # a real copy
c.append(5)
print(a, c)

Output

[1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4, 5]

Lists inside lists

seats = [
    ["Ana", "Ben", "Carla"],
    ["Dan", "Ela", "Fe"],
]
print(seats[1][0])    # row 1, seat 0

Output

Dan

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which method adds an item to the end of a list?
2. What is [10, 20, 30][-1]?
3. What happens with lst.remove("x") if "x" isn't in the list?
4. After a = [1]; b = a; b.append(2), what is a?

By Sir Reyel · Last updated 2026-12-04