Python Lists — Tutorial with Examples
Master Python lists in 7 minutes. Indexing, slicing, the most-used methods, and a runnable browser editor so you can practice without installing anything.
Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.
A list in Python is an ordered, mutable collection. You create one with square brackets:
fruits = ["apple", "banana", "cherry"]
scores = [85, 92, 78, 95, 88]
mixed = ["alice", 30, True] # legal but discouraged in real code
Lists are *the* workhorse data structure of Python — you'll see them everywhere.
Indexing
Python uses zero-based indexing. fruits[0] is "apple". Even more useful: negative indexing wraps from the end. fruits[-1] is "cherry", fruits[-2] is "banana". No need to write fruits[len(fruits) - 1] ever again.
Slicing
fruits[start:stop] gives you a sub-list. The stop index is exclusive.
nums = [10, 20, 30, 40, 50]
nums[1:3] # [20, 30]
nums[:3] # [10, 20, 30] — from start
nums[2:] # [30, 40, 50] — to end
nums[::-1] # [50, 40, 30, 20, 10] — reversed in one line
The most-used list methods
| Method | What it does |
|---|---|
.append(x) | Add x to the end |
.insert(i, x) | Insert at position i |
.pop() | Remove and return last item |
.pop(0) | Remove and return first item *(slow on large lists)* |
.remove(value) | Remove first matching value |
.sort() | Sort in place |
.reverse() | Reverse in place |
len(nums) | How many items |
value in nums | Membership check (returns True/False) |
Lists vs tuples — when to use which
A tuple is the immutable cousin of a list (you'll meet them on Day 1 too). The rule: use a tuple when the size and meaning is fixed (a coordinate, an RGB color, a database row). Use a list when items will be added, removed, or reordered.
Common mistakes
- List as default argument: never write
def add(item, basket=[]). The list is created *once* and reused across every call to the function. Usebasket=Noneand check inside. .pop(0)on big lists: O(n). Usecollections.dequeif you need a fast queue.- Mistaking shallow copy for deep copy:
b = adoesn't copy —bandapoint to the same list. Useb = a.copy()(orlist(a)) for a shallow copy.
Where this fits in the 7-Day Python Sprint
Lists are introduced on Day 1 and used throughout the sprint — from the contact-book lab on Day 1 to the email analyzer on Day 7.
Practice
Auto-graded — run in your browser, no signup.
Exercise 1: Add `'mango'` to the `fruits` list, then assign the second item to a variable `second`.
Runs in your browser via Pyodide — no signup, no install. Want auto-graded full exercises + videos? See the 7-Day Python Sprint.
Like this tutorial? Get the full Python Sprint.
7 days, daily videos, auto-graded labs, AI tutor, mini-project. 1-year content access.
See the Python Sprint