fuckingpython.lol

Learn Python › Lesson 6

Python lists, where the first item is number zero. Deal with it.

Updated · every example run on Python 3.9 to 3.15

So far every variable has held one thing: one balance, one cat, one sad battery percentage. Real life comes in piles. A shopping list. The cat's weight every month. Your bank statement, unfortunately. You could make item1, item2, item3, and you'd hate yourself by item14.

A list is one variable that holds as many values as you like, in order. It's the container you'll use most in Python, and it has exactly one rule that bites: counting starts at zero.

Make a list

Square brackets, items separated by commas:

main.py
groceries = ["milk", "eggs", "bread", "coffee"]
print(groceries)
print(len(groceries))
print(type(groceries))

nothing = []
print(nothing, len(nothing))

mixed = ["Mittens", 4.8, True, None]
print(mixed)
Python 3.9 to 3.15 all print
['milk', 'eggs', 'bread', 'coffee']
4
<class 'list'>
[] 0
['Mittens', 4.8, True, None]

print shows a list the way you'd type it, quotes and all. len counts the items, the same len you used on strings. An empty list is [], and it's falsy, as promised last lesson: if groceries: means "if there's anything on the list".

A list can mix types, like mixed does. It's allowed. It's also usually a sign your program has been through some shit. A list of one kind of thing is far easier to work with.

Indexing: the first item is number zero

Put a number in square brackets after the list to get one item out:

main.py
groceries = ["milk", "eggs", "bread", "coffee"]

print(groceries[0])
print(groceries[1])
print(groceries[3])
print(groceries[-1])
print(groceries[-2])
Python 3.9 to 3.15 all print
milk
eggs
coffee
coffee
bread

Think of the index as "how many items to skip from the front". The first item skips none, so it's 0. Four items means indexes 0 to 3, and there is no groceries[4], which we'll prove at the end of this lesson, painfully.

Negative indexes count from the back: -1 is the last item, -2 the one before it. You don't need to know how long the list is, which is the whole point. Same rules as indexing a string.

Slicing: a piece of the list

Two numbers with a colon give you a slice, a new list with part of the old one:

main.py
groceries = ["milk", "eggs", "bread", "coffee", "cat food"]

print(groceries[1:3])
print(groceries[:2])
print(groceries[2:])
print(groceries[-2:])
print(groceries[1:100])
print(groceries[::-1])
Python 3.9 to 3.15 all print
['eggs', 'bread']
['milk', 'eggs']
['bread', 'coffee', 'cat food']
['coffee', 'cat food']
['eggs', 'bread', 'coffee', 'cat food']
['cat food', 'coffee', 'bread', 'eggs', 'milk']

The start is included, the stop isn't: [1:3] is items 1 and 2. Leave out the start and the slice begins at the front; leave out the stop and it runs to the end. A third number is the step, and a step of -1 walks backwards, which hands you a reversed copy and leaves the original as it was.

Note [1:100]. A slice that runs past the end doesn't fail, it just gives you what's there. An index that runs past the end is another story.

Lists can change

Strings are immutable: you can't change a letter in place. Lists are the opposite. You can replace an item, add one to the end, or shove one in anywhere:

main.py
groceries = ["milk", "eggs", "bread"]

groceries[0] = "oat milk"
print(groceries)

groceries.append("coffee")
print(groceries)

groceries.insert(0, "cat food")
print(groceries)
Python 3.9 to 3.15 all print
['oat milk', 'eggs', 'bread']
['oat milk', 'eggs', 'bread', 'coffee']
['cat food', 'oat milk', 'eggs', 'bread', 'coffee']

append(x) adds x to the end. insert(i, x) puts x at index i and moves everything from there on one place along. The cat food goes first. The cat checked.

These are methods, called with a dot like the string methods. One difference that matters soon: string methods hand you back a new string, while append and insert change the list itself and hand you back None, Python's word for nothing.

There is no push

If you've touched JavaScript, your fingers will type push. Python has never heard of it:

main.py
groceries = ["milk", "eggs"]
groceries.push("coffee")
print(groceries)
Python 3.15 prints
Traceback (most recent call last):
  File "/home/me/project/main.py", line 2, in <module>
    groceries.push("coffee")
    ^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'push'. Did you mean '.append'?
PythonPrints
3.9–3.14AttributeError: 'list' object has no attribute 'push'
3.15AttributeError: 'list' object has no attribute 'push'. Did you mean '.append'?

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.

Every version says the list object has no attribute push, and the last line never runs. Python 3.15 is the first to add a hint and ask whether you meant .append. The older ones leave you to figure that shit out yourself. More at AttributeError: 'list' object has no attribute.

Taking things out: remove and pop

remove deletes by value: you say what. pop deletes by position: you say where, and it hands you the item back:

main.py
groceries = ["milk", "eggs", "bread", "eggs", "coffee"]

groceries.remove("eggs")
print(groceries)

last = groceries.pop()
print(last)
print(groceries)

first = groceries.pop(0)
print(first, groceries)
Python 3.9 to 3.15 all print
['milk', 'bread', 'eggs', 'coffee']
coffee
['milk', 'bread', 'eggs']
milk ['bread', 'eggs']

remove("eggs") took out the first "eggs" only; the second one survived. pop() with no number takes the last item, pop(0) the first.

Both get upset when there's nothing to take. Removing something that isn't on the list raises a ValueError, and popping an empty list raises an IndexError. Which is why you check first.

in: is it on the list?

in asks whether any item equals the thing you name. It answers True or False, so it slots straight into an if:

main.py
groceries = ["milk", "eggs", "bread"]

print("eggs" in groceries)
print("Eggs" in groceries)
print("egg" in groceries)
print("egg" in "eggs")
print("beer" not in groceries)

if "coffee" in groceries:
    print("Coffee is handled.")
else:
    print("No coffee on the list. Fixing that.")
    groceries.append("coffee")

print(groceries)
Python 3.9 to 3.15 all print
True
False
False
True
True
No coffee on the list. Fixing that.
['milk', 'eggs', 'bread', 'coffee']

The match is exact, capitals included. And watch the two "egg" lines: on a string, in looks for a piece of text, so "egg" is in "eggs". On a list, it wants a whole item, and there is no item called "egg". Same word, different question.

if "eggs" in groceries: before groceries.remove("eggs") is how you remove things without blowing up.

sort or sorted

Two ways to put a list in order, and they behave differently in one important, annoying way:

main.py
cat_weights = [5.2, 4.1, 4.8, 4.3]

in_order = sorted(cat_weights)
print(in_order)
print(cat_weights)

cat_weights.sort()
print(cat_weights)

cat_weights.sort(reverse=True)
print(cat_weights)

result = cat_weights.sort()
print(result)
Python 3.9 to 3.15 all print
[4.1, 4.3, 4.8, 5.2]
[5.2, 4.1, 4.8, 4.3]
[4.1, 4.3, 4.8, 5.2]
[5.2, 4.8, 4.3, 4.1]
None

sorted() makes a new, sorted list and leaves the original alone. .sort() sorts the list itself and returns None. Both take reverse=True for biggest first.

That None is the trap. Write groceries = groceries.sort() and you've thrown your list away and kept a None. The next groceries[0] then fails with TypeError: 'NoneType' object is not subscriptable, and the next groceries.append("tea") with AttributeError: 'NoneType' object has no attribute 'append'. Sort in place, or assign the result of sorted(). Never assign the result of .sort().

Strings sort by the same character-by-character rules as last lesson's comparisons, so "Zebra" lands before "apple". And a list that mixes numbers and strings can't be sorted at all: Python raises a TypeError rather than decide whether 3 comes before "apple".

IndexError: one past the end

Here's the mistake every damn programmer makes, beginners and veterans alike. Four items, so the last index is 3, but len says 4:

main.py
groceries = ["milk", "eggs", "bread", "coffee"]
print("Items:", len(groceries))
print("Last one:", groceries[len(groceries) - 1])
print(groceries[len(groceries)])
Python 3.15 prints
Items: 4
Last one: coffee
Traceback (most recent call last):
  File "/home/me/project/main.py", line 4, in <module>
    print(groceries[len(groceries)])
          ~~~~~~~~~^^^^^^^^^^^^^^^^
IndexError: list index out of range
PythonPrints
3.9–3.15IndexError: list index out of range

Run on CPython 3.9.6, 3.10.22, 3.11.17, 3.12.15, 3.13.17, 3.14.9 and 3.15.1, on November 11, 2026.

The first two prints worked. The third asked for a fifth item that doesn't exist, and Python stopped with IndexError: list index out of range. Every version from 3.9 to 3.15 words it the same way; from 3.11 on, the traceback also underlines the exact bit of the line that failed.

The last item is len(groceries) - 1, or, much less painfully, groceries[-1].

Try it yourself

A vet's waiting room: waiting = ["Mittens", "Biscuit", "Pickles"]. A dog called Rex arrives and joins the back of the line. Mittens is called in: take the first name off the list and print it. Biscuit panics and goes home: take him off by name. Print who's left and how many.

Show the answer
waiting = ["Mittens", "Biscuit", "Pickles"]

waiting.append("Rex")
called = waiting.pop(0)
print("Next:", called)
waiting.remove("Biscuit")
print(waiting, len(waiting))

pop(0) both removes Mittens and hands her back, so one line does the taking and the remembering. Pickles and Rex are left, two of them.

Questions

How do I copy a list in Python?

Use new = old.copy() or new = old[:]. A plain new = old doesn't copy anything: it gives the same list a second name, so appending through one name shows up under the other.

What is the difference between append and extend?

append adds one item. extend adds every item of another list, one by one. Append a list, and the whole list goes in as a single item, a list inside your list.

How do I get the last item of a list in Python?

groceries[-1]. It works whatever the length, as long as the list isn't empty; an empty list has no last item and raises an IndexError.

Why do Python lists start at 0?

Because an index is a distance from the start, and the first item is zero steps away. Most programming languages count the same way, so the habit you build here travels with you.

Sources

Read on November 11, 2026.