What you'll learn
A single variable holds one value — but programs handle many. The list is Python's workhorse: an ordered, growable, changeable collection you'll reach for constantly. This module makes you fluent with it.
By the end you'll be able to:
- Create lists and access elements by index and slice
- Add, remove, and change items (lists are mutable)
- Iterate lists and use built-ins like
sum,max,len - Understand the crucial difference between a reference and a copy
- Sort with
.sort(),sorted(), and akey
Creating & indexing
A list is written in square brackets, with items separated by commas. It can hold any types (even mixed), and everything you learned about string indexing and slicing works here too:
A list is an ordered, growable sequence. Index from 0 (top) or from the end with negatives (bottom). Unlike a tuple, a list can be changed after creation.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
print(fruits[-1]) # cherry (last)
print(fruits[0:2]) # ['apple', 'banana'] (slice)
print(len(fruits)) # 3
print("banana" in fruits) # TrueMethods & mutability
Unlike strings and tuples, lists are mutable — you can change them in place. They come with a rich set of methods for adding and removing items:
nums = [3, 1, 4]
nums.append(1) # add to the end -> [3, 1, 4, 1]
nums.insert(0, 5) # insert at index -> [5, 3, 1, 4, 1]
nums.remove(1) # remove first 1 -> [5, 3, 4, 1]
last = nums.pop() # remove & return the last item
nums[0] = 99 # lists are MUTABLE
print(nums)
print("popped:", last)| Method | What it does |
|---|---|
.append(x) | add x to the end |
.insert(i, x) | insert x at index i |
.remove(x) | remove the first x |
.pop(i) | remove & return item at i (default: last) |
.sort() / .reverse() | reorder the list in place |
.index(x) / .count(x) | find / count x |
Iterating over lists
Lists and for loops are a perfect pair — loop straight over the items. Python's built-in functions handle common jobs for you:
scores = [90, 85, 100, 70]
for score in scores:
print(score, end=" ")
print()
print("Total:", sum(scores)) # 345
print("Max:", max(scores)) # 100References vs copies
This is the single most important thing to understand about lists (and all mutable objects). A variable holds a reference to the list, not the list itself. So b = a does not copy — both names point to the same list:
b = a
Both names point to the same list. Change one, change both.
c = a.copy()
A real copy — independent lists.
a = [1, 2, 3]
b = a # NOT a copy — b is the SAME list as a
b.append(4)
print(a) # [1, 2, 3, 4] <- a changed too!
c = a.copy() # a real (shallow) copy
c.append(5)
print(a) # [1, 2, 3, 4] <- unchanged
print(c) # [1, 2, 3, 4, 5]Watch out
.copy() (or list(a), or a[:]). Note these are shallow copies — for lists of lists, use copy.deepcopy() to copy the inner lists too.Sorting
Two ways to sort: .sort() reorders the list in place and returns None; the built-in sorted() leaves the original alone and returns a new sorted list. Both accept a key function and a reverse flag:
nums = [3, 1, 4, 1, 5, 9, 2]
nums.sort() # sorts IN PLACE
print(nums)
words = ["banana", "kiwi", "apple"]
print(sorted(words, key=len)) # sorted() returns a NEW list, by length
print(sorted(words, reverse=True)) # reverse alphabeticalTip
key is a function that decides what to sort by — key=len sorts by length, key=str.lower sorts case-insensitively. Combined with a lambda (Module 7), you can sort by anything.Recap & quick check
Key takeaways
- Lists are ordered, growable, and mutable; index from 0 or negatively, and slice like strings.
- Methods like append, insert, remove, and pop change the list in place.
- A variable holds a reference: b = a shares one list — use .copy() for an independent one.
- Shallow copies don't copy nested lists; use copy.deepcopy() for that.
- .sort() reorders in place (returns None); sorted() returns a new list — both take key and reverse.
Quick check
1. What does 'b = a' do when a is a list?
2. Which method adds an item to the end of a list?
3. What does list.sort() return?
4. How do you sort a list by a custom rule, like length?
5. Are lists mutable?
Great — the list is now second nature. Next up: Module 9 — Tuples & Sequences, the immutable cousin of the list.