📋
Coding🔬 Ages 11-13Beginner 12 min read

Python Lists in Depth

Go deeper with Python lists: indexing, slicing, append, insert, remove, sort and reverse, len, and looping with for. Master the list methods with runnable code, a worked example and a quiz.

Key takeaways

  • A list stores many values in order, and the first position is index 0
  • Lists are mutable: you can change, add and remove items after creating them
  • append() adds to the end, insert() adds at a position, remove() deletes by value
  • Slicing with [start:stop] copies a section of the list
  • len() tells you how many items a list has, and a for loop visits each one

Why lists matter so much

A single variable holds one value. But programs usually deal with collections: a class of students, the scores in a game, the items in a shopping basket. A list lets one variable hold many values, kept in a definite order, and Python gives you a rich set of tools to work with them.

You may have already met lists briefly in loops and lists. This lesson goes properly in depth: indexing, slicing, and the methods that add, remove and reorder items.

Creating a list and reading items

You write a list with square brackets and commas:

colours = ["red", "green", "blue"]
print(colours)        # ['red', 'green', 'blue']
print(colours[0])     # red
print(colours[2])     # blue
  • colours = [...] creates the list with three items.
  • colours[0] reads the first item. This is the key idea: Python numbers positions starting from 0, not 1. So colours[0] is "red" and colours[2] is "blue".

You can also count from the end using negative indexes:

print(colours[-1])    # blue   (last item)
print(colours[-2])    # green  (second from the end)

-1 always means the last item, which saves you from working out the length first.

Changing items

Lists are mutable, meaning you can change them after they're made. To replace an item, assign to its index:

colours = ["red", "green", "blue"]
colours[1] = "yellow"
print(colours)        # ['red', 'yellow', 'blue']

Here colours[1] = "yellow" overwrites the item at position 1. The list itself stays the same object — only its contents changed.

Adding items: append and insert

To grow a list, two methods are key.

append() adds an item to the end:

basket = ["milk", "eggs"]
basket.append("bread")
print(basket)         # ['milk', 'eggs', 'bread']

insert(position, item) adds at a specific position, pushing the rest along:

basket.insert(0, "butter")
print(basket)         # ['butter', 'milk', 'eggs', 'bread']
  • basket.append("bread") puts "bread" at the end.
  • basket.insert(0, "butter") puts "butter" at position 0, so everything else shifts one place right.

Notice the dot: basket.append(...) calls a method on the list basket. The method changes the list directly and returns None, so never write basket = basket.append(...).

Removing items: remove and pop

remove(value) deletes the first matching value:

basket = ["butter", "milk", "eggs", "bread"]
basket.remove("milk")
print(basket)         # ['butter', 'eggs', 'bread']

pop(index) removes the item at a position and gives it back to you:

last = basket.pop()       # no index means the last item
print(last)               # bread
print(basket)             # ['butter', 'eggs']
  • remove("milk") searches for "milk" and deletes it.
  • pop() with no number removes and returns the last item, storing it in last.

Slicing: taking a section

A slice copies part of a list using [start:stop], following the same "up to but not including" rule as range():

nums = [10, 20, 30, 40, 50]
print(nums[1:3])     # [20, 30]
print(nums[:2])      # [10, 20]    from the start
print(nums[3:])      # [40, 50]    to the end
  • nums[1:3] takes index 1 and 2, stopping before 3, giving [20, 30].
  • Leaving the left side blank ([:2]) means "from the beginning."
  • Leaving the right side blank ([3:]) means "to the very end."

Slicing makes a new list and leaves the original untouched.

Sorting and reversing

Two more methods reorder a list in place:

scores = [50, 90, 20, 70]
scores.sort()
print(scores)        # [20, 50, 70, 90]

scores.reverse()
print(scores)        # [90, 70, 50, 20]
  • sort() rearranges the items from smallest to largest (or alphabetically, for text).
  • reverse() flips the order. Combining them, as above, gives a largest-first ordering.

Like append, these change the list directly and return None.

Length and looping

len() tells you how many items a list holds, and a for loop visits each one:

animals = ["cat", "dog", "fish"]
print(len(animals))   # 3

for animal in animals:
    print("I see a", animal)
  • len(animals) returns 3.
  • The loop sets animal to each item in turn, printing I see a cat, then dog, then fish. This connects directly to what you learned in python loops and lists.

Worked example: a high-score table

Let's combine these skills into a small high-score tracker.

scores = [320, 145, 580, 210]

# A new score comes in
scores.append(450)

# Sort highest first
scores.sort()
scores.reverse()

print("All scores:", scores)
print("Top score:", scores[0])
print("Top three:", scores[:3])
print("Number of scores:", len(scores))

How it works:

  1. scores.append(450) adds the new score to the end of the list.
  2. sort() puts them in ascending order, then reverse() flips it so the highest is first.
  3. scores[0] reads the top score, and the slice scores[:3] grabs the top three.
  4. len(scores) reports how many scores there are in total.

The output is a tidy leaderboard, built entirely from list operations:

All scores: [580, 450, 320, 210, 145]
Top score: 580
Top three: [580, 450, 320]
Number of scores: 5

Try it yourself

Build a simple to-do list manager:

  • Start with tasks = ["wash up", "homework", "feed cat"].
  • append() a new task, "tidy room".
  • remove() the task "homework" because it's done.
  • insert() "make breakfast" at position 0 so it comes first.
  • Print the final list and its length with len().

Then loop over the list and print each task with a number in front, like 1. make breakfast. (Hint: a for loop with range(len(tasks)) gives you both the position and the item.) When you're ready for collections that pair a key with a value, explore dictionaries in Python.

Quick quiz

Test yourself and earn XP

In the list ['a', 'b', 'c'], what index is 'a' at?

What does append() do to a list?

What does colours[-1] give for colours = ['red', 'green', 'blue']?

What does the slice nums[1:3] return for nums = [10, 20, 30, 40]?

What does len([5, 6, 7, 8]) return?

FAQ

Mutable means changeable. After you create a list you can add items, remove items, or replace an item at a position, and it's still the same list. This is different from strings and numbers, which are immutable — operations on them create brand-new values rather than changing the original. Because lists are mutable, methods like append() and sort() change the list directly, and they don't return a new list.

Slicing follows the same 'up to but not including' rule as range(). nums[1:3] takes index 1 and index 2, but stops before index 3. This is handy because the number of items in the slice is simply stop minus start, here 3 - 1 = 2 items. You can also leave a side blank: nums[:2] means from the start, and nums[2:] means to the end.