Skip to main content
Albero Academy

Lists

Python's ordered, mutable sequence — the workhorse collection.

Data Structures12 min

A list is an ordered, changeable collection of items — the most-used data structure in Python. It can hold any types (even mixed), grow and shrink, and be sorted and sliced. If you're keeping a sequence of things, it's probably a list.

Indexing & slicing

Access items by position (starting at 0), count from the end with negatives, and take sub-lists with slices:

reading a list
nums = [3, 1, 4, 1, 5]
nums[0] # 3 first item
nums[-1] # 5 last item
nums[1:3] # [1, 4] slice: indices 1,2 (stop exclusive)
nums[:2] # [3, 1] first two
nums[::-1] # [5, 1, 4, 1, 3] reversed
len(nums) # 5 length

Adding & removing

Lists are mutable — these methods change the list in place:

MethodDoes
append(x)Add x to the end
insert(i, x)Insert x at position i
remove(x)Remove the first x
pop(i)Remove & return item at i (last if omitted)
sort() / reverse()Reorder in place
mutating a list
nums = [3, 1, 4]
nums.append(9) # [3, 1, 4, 9]
nums.insert(0, 0) # [0, 3, 1, 4, 9]
nums.pop() # returns 9 → [0, 3, 1, 4]
nums.sort() # [0, 1, 3, 4]
nums.sort(reverse=True) # [4, 3, 1, 0]

Lists are references

the aliasing trap
a = [1, 2, 3]
b = a # same list!
b.append(4)
print(a) # [1, 2, 3, 4] — a changed too
c = a.copy() # independent copy
c.append(5)
print(a) # [1, 2, 3, 4] — unchanged

Key takeaways

  1. 01Lists are ordered and mutable; index (0-based, negatives from the end) and slice like strings.
  2. 02append/insert/remove/pop modify in place; sort()/reverse() reorder in place.
  3. 03Lists can hold mixed types and nest arbitrarily.
  4. 04Assignment shares the reference — use .copy() when you need an independent list.

Finished this chapter?

Mark it complete to track your progress through Python.