Courses/Python Mastery/Module 5: Data Structures
Module 5 · Lesson 125 minBeginner

Lists: The Workhorse

Lesson goal
Store collections, index, slice, append, and sort — the structure you'll use most.

Lists: The Workhorse

If variables are boxes, lists are shelves of boxes — one name holding many values, in order. You'll use lists in literally every project from here to the end of the course.

Creating and reading

code
languages = ["Python", "Go", "Rust"]
marks = [85, 92, 78, 90]
mixed = ["Aarav", 16, True]        # allowed, but rare in practice

print(languages[0])     # Python  ← index starts at 0
print(languages[2])     # Rust
print(languages[-1])    # Rust    ← negative = from the end
print(len(languages))   # 3

Slicing — grabbing sections

Same slice syntax as strings: start:stop, stop excluded:

code
marks = [85, 92, 78, 90, 66, 88]

print(marks[1:3])     # [92, 78]      positions 1 and 2
print(marks[:2])      # [85, 92]      first two
print(marks[-2:])     # [66, 88]      last two
print(marks[::-1])    # reversed!     the famous trick

Modifying lists — the five essential moves

code
todo = ["homework", "gym"]

todo.append("sleep")           # add to the END
todo.insert(0, "wake up")      # insert AT a position
todo.remove("gym")             # remove by VALUE (first match)
last = todo.pop()              # remove & return the LAST item
todo[0] = "meditate"           # overwrite by index

print(todo)

Useful built-ins

code
marks = [85, 92, 78, 90]

print(sum(marks))       # 345
print(max(marks))       # 92
print(min(marks))       # 78
print(len(marks))       # 4
print(sorted(marks))    # [78, 85, 90, 92] — NEW sorted list
print(92 in marks)      # True — membership

Note sorted(marks) returns a new list; marks.sort() sorts in place (changes the original, returns None). Different tools, different habits:

code
marks.sort()            # modifies marks itself
marks.reverse()         # also in place

Lists + loops: the power couple

code
fruits = ["apple", "banana", "mango"]

for fruit in fruits:
    print(f"I like {fruit}")

# With index (when you need the position too):
for i, fruit in enumerate(fruits):
    print(f"{i + 1}. {fruit}")

Output:

code
I like apple
I like banana
I like mango
1. apple
2. banana
3. mango

enumerate gives you both the position and the value — the professional way to number things (no manual counters).

Lists are mutable — and that has consequences

Unlike strings, lists change in place. This creates the most surprising behavior in beginner Python:

code
a = [1, 2, 3]
b = a               # b points at the SAME list — no copy!
b.append(4)
print(a)            # [1, 2, 3, 4]  ← a changed too!

To make a real copy: b = a.copy() or b = a[:]. You met this in the operators practice — now you know the full story.

Common Errors & Fixes

  • `IndexError: list index out of range` — asking for position 5 in a 3-item list. Valid indexes: 0 to len-1.
  • `ValueError: 'x' not in list`.remove("x") on something absent. Check first: if "x" in todo:.
  • "My sorted() didn't work"sorted(lst) returns a new list; lst.sort() changes in place and returns None. Don't write lst = lst.sort()!

  • ✅ Checkpoint

  • What's marks[-1] of [85, 92, 78]? *(78)*
  • Difference between append and insert? *(End vs specific position)*
  • sorted(lst) vs lst.sort()? *(New list vs in-place)*
  • Why did modifying b change a? *(b = a copies the reference, not the list)*
  • Next: tuples and sets — two specialized containers that solve specific problems.