Learn Without Walls
← Back to Module 6

Module 6: Lists -- Study Guide

Review all key concepts from Module 6. Use this guide to prepare for the quiz or as a reference while coding.

1. What is a List?

Definition: A list is an ordered, mutable collection of items. Lists can contain any data type and allow duplicate values.

Key characteristics:

# Creating lists
empty = []                           # Empty list
fruits = ["apple", "banana"]        # String list
mixed = [1, "hello", True, 3.14]   # Mixed types
from_range = list(range(5))       # [0, 1, 2, 3, 4]

2. Indexing

Rule: Indexing starts at 0. The first element is at index 0, the second at index 1, and so on. Negative indexes count from the end: -1 is the last element.
colors = ["red", "green", "blue", "yellow"]
#           0       1        2        3
#          -4      -3       -2       -1

colors[0]    # "red"
colors[-1]   # "yellow"
colors[-2]   # "blue"
Watch out: Accessing an index beyond the list length causes an IndexError. For a list of length 4, valid indexes are 0 to 3 (or -1 to -4).

3. List Methods

Remember: most list methods modify the list in place and return None.

Adding Items

Removing Items

Ordering

Searching

Common mistake: Writing my_list = my_list.sort() sets my_list to None because sort() returns None. Just write my_list.sort() by itself.

4. Slicing

Syntax: list[start:stop:step] -- start is inclusive, stop is exclusive, step is the increment.

Key patterns to remember:

Important: copy = original does NOT create a copy. Both variables point to the same list. Use copy = original[:] or copy = original.copy() for a true copy.

5. List Comprehensions

Purpose: A concise way to create new lists from existing sequences in a single line.

Three Patterns

# 1. Basic: [expression for item in iterable]
squares = [x**2 for x in range(10)]

# 2. With filter: [expression for item in iterable if condition]
evens = [x for x in range(20) if x % 2 == 0]

# 3. With if/else: [A if condition else B for item in iterable]
labels = ["pass" if s >= 60 else "fail" for s in scores]

When NOT to use comprehensions: complex multi-step logic, side effects (printing, file I/O), or when readability suffers.

6. Review Questions

Q1: What is the index of the first element in a Python list?

Q2: What is the difference between append() and extend()?

Q3: What does sort() return?

Q4: How do you create a true copy of a list?

Q5: What does lst[::2] return?

Q6: Write a list comprehension that creates a list of even numbers from 0 to 20.

Q7: What happens if you use remove() on an item that is not in the list?

Q8: What is the difference between sort() and sorted()?

7. Common Patterns to Remember

# Check before removing
if item in my_list:
    my_list.remove(item)

# Check before indexing
if item in my_list:
    pos = my_list.index(item)

# Safe copy
copy = original[:]

# Get last element safely
last = my_list[-1] if my_list else None

# Average of a list
avg = sum(numbers) / len(numbers)
Take the Module Quiz → Quick Reference Card