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?
Key characteristics:
- Ordered -- items maintain their insertion order
- Mutable -- you can add, remove, and change items after creation
- Indexed -- each item has a position number starting at 0
- Heterogeneous -- can contain mixed data types
# 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
colors = ["red", "green", "blue", "yellow"] # 0 1 2 3 # -4 -3 -2 -1 colors[0] # "red" colors[-1] # "yellow" colors[-2] # "blue"
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
append(x)-- adds x to the endinsert(i, x)-- inserts x at index iextend(iterable)-- adds each item from iterable
Removing Items
remove(x)-- removes first occurrence of x (ValueError if not found)pop()-- removes and returns last itempop(i)-- removes and returns item at index iclear()-- removes all items
Ordering
sort()-- sorts ascending in place (returns None)sort(reverse=True)-- sorts descending in placereverse()-- reverses order in place
Searching
count(x)-- returns number of times x appearsindex(x)-- returns index of first x (ValueError if not found)
my_list = my_list.sort() sets my_list to None because sort() returns None. Just write my_list.sort() by itself.
4. Slicing
list[start:stop:step] -- start is inclusive, stop is exclusive, step is the increment.
Key patterns to remember:
lst[:n]-- first n elementslst[n:]-- everything from index n onwardlst[-n:]-- last n elementslst[:]-- copy the entire listlst[::2]-- every other elementlst[::-1]-- reversed copylst[1:-1]-- everything except first and last
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
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)