# Write your code here!
Appendix C — Collections: Practice Problems
These are the practice problems for Chapter 2. For solutions, see Appendix D.
C.1 Creating and Accessing Lists
Task Description
Create a list called gene_names
containing the strings "TP53"
, "BRCA1"
, and "MDM2"
. Then, print out each gene name by accessing its items one at a time using their indices.
Learning Objectives
- Create a list with multiple items
- Access list elements by index
- Print list elements
Solution
Test Cases
# Should print:
# TP53
# BRCA1
# MDM2
Common Issues
- Forgetting to use square brackets for list creation or indexing
- Starting index at 1 instead of 0
Optional Extensions
- Use a for loop to print all gene names
C.2 Slicing Lists
Task Description
Given the list bases = ["A", "T", "G", "C", "A", "T"]
, use slicing to create: - first_three
, the first three items, - last_two
, the last two items.
Print both variables.
Learning Objectives
- Use list slicing to extract parts of a list
Solution
# Write your code here!
Test Cases
# Output:
# ['A', 'T', 'G']
# ['G', 'C']
Common Issues
- Off-by-one errors in slicing
- Misusing negative indices
Optional Extensions
- Print the middle two items of the list
- Practice getting different sized slices from the end of the collection
C.3 Using List Methods: append
and remove
Task Description
Start with genes = ["TP53", "BRCA1"]
. Add "ATM"
to the end of the list, then remove "BRCA1"
. Print the resulting list.
Learning Objectives
- Modify lists by adding and removing items
- Use
append()
andremove()
list methods
Solution
# Write your code here!
Test Cases
# Output:
# ['TP53', 'ATM']
Common Issues
- Trying to remove an item not present in the list
- Using list methods as built-in functions (e.g.,
append(numbers, 3)
rather thannumbers.append(3)
)
Optional Extensions
- Add another gene at the start using
insert()
C.4 Creating a Tuple
Task Description
Store the chromosome location for a gene, which starts at position 2_345_678
and ends at 2_346_987
, as a tuple. Print the tuple.
Learning Objectives
- Create and print a tuple to store related information
Solution
# Write your code here!
Test Cases
# Output:
# (2345678, 2346987)
Common Issues
- Using brackets instead of parentheses
Optional Extensions
- Print the start and end positions separately by indexing the tuple
- Try to reassign (change) the first element of the tuple to another value. What happens?
C.5 Dictionary Creation and Value Lookup
Task Description
Create a dictionary named gene_functions
mapping the gene "TP53"
to "tumor suppression"
and "BRCA1"
to "DNA repair"
. Then, use the dictionary to print the function of "TP53"
.
Learning Objectives
- Create dictionaries with key-value pairs
- Access dictionary values using keys
Solution
# Write your code here!
Test Cases
# Output:
# tumor suppression
Common Issues
- Using parentheses or square brackets instead of curly brackets for dictionary literals
- Misspelling keys
Optional Extensions
- Try to print the value for a gene that doesn’t exist. What happens?
C.6 Using Dictionary get
Method
Task Description
Use the gene_functions
dictionary from the previous task. Retrieve the function for "MDM2"
safely using the get
method, with a default value of "Unknown"
. Print the result.
Learning Objectives
- Use the
get()
method to safely access dictionary values
Solution
# Write your code here!
Test Cases
# Output:
# Unknown
Common Issues
- Not exactly an “issue” per se, but, forgetting the default value for
get
, leading toNone
- Confusing square bracket dictionary access and the
get()
method
Optional Extensions
- Add
"MDM2": "p53 regulation"
to the dictionary, then useget
again
C.7 Looping Through a List
Task Description
Given expression = [2.3, 3.1, 1.9]
, write a for loop that prints each value in the list.
Learning Objectives
- Use a for loop to iterate over a list
Solution
# Write your code here!
Test Cases
# Output:
# 2.3
# 3.1
# 1.9
Common Issues
- Forgetting indentation within the loop block
- Using the wrong variable name inside the loop
Optional Extensions
- Print the index and value together using
enumerate()
C.8 Counting Items with a Loop
Task Description
Given the string sequence = "AAGCTTAA"
, count how many times "A"
appears by looping through the string and incrementing a counter variable. Print the count.
Learning Objectives
- Use a loop and a counter to count characters
Solution
# Write your code here!
Test Cases
# Output:
# 4
Common Issues
- Counting every base rather than only the adenines
- Using
base = "A"
instead ofbase == "A"
Optional Extensions
- Use the string method
count()
to check your answer
C.9 Filtering a List with a For Loop
Task Description
Given a list genes = ["TP53", "BRCA1", "MDM2", "ATM"]
, create a new list named genes_with_A
containing only gene names that include the letter 'A'
. Print this filtered list.
Learning Objectives
- Use a for loop with a conditional to filter and build a new list
Solution
# Write your code here!
Test Cases
# Output:
# ['BRCA1', 'ATM']
Common Issues
- Forgetting to initialize the new list before the loop
- Appending all items, not just those with ‘A’
Optional Extensions
- Solve the same task using a list comprehension
C.10 Using the in
Operator
Task Description
Given a list of gene names genes = ["TP53", "BRCA1", "MDM2"]
, check if "BRCA1"
is present in the list, and print "Found"
if it is, or "Not found"
otherwise.
Learning Objectives
- Use the
in
operator to test list membership
Solution
# Write your code here!
Test Cases
# Output:
# Found
Common Issues
- Checking the wrong value or casing
Optional Extensions
- Prompt the user to enter a gene name and check if it’s in the list
C.11 Nested Lists and Indexing
Task Description
Given the nested list:
= [
samples "Sample1", [1.1, 1.2, 1.3]],
["Sample2", [2.1, 2.2, 2.3]],
["Sample3", [3.1, 3.2, 3.3]],
[ ]
Print the value 3.1
by correctly indexing into samples
.
Learning Objectives
- Index into nested lists
Solution
# Write your code here!
Test Cases
# Output:
# 3.1
Common Issues
- Mixing up list indices
- Using the 1-based indexing rather than 0-based indexing (especially if you’re used to the R programming language)
Optional Extensions
- Print all values for “Sample2”
- Use nested
for
loops with enumerate to print values with their indices
C.12 Building a Dictionary from Two Lists
Task Description
You are given:
= ["nrdA", "nrdJ", "nrdD"]
gene_ids = ["Class I RNR", "Class II RNR", "Class III RNR"] products
Build a dictionary mapping each gene id to its product using zip()
and a for
loop. Print the resulting dictionary.
Learning Objectives
- Loop through multiple lists
- Build a dictionary programmatically
Solution
= ["nrdA", "nrdJ", "nrdD"]
gene_ids = ["Class I RNR", "Class II RNR", "Class III RNR"]
products
= {}
gene_products for i in range(len(gene_ids)):
= products[i]
gene_products[gene_ids[i]]
print(gene_products)
{'nrdA': 'Class I RNR', 'nrdJ': 'Class II RNR', 'nrdD': 'Class III RNR'}
Test Cases
# Output:
# {'nrdA': 'Class I RNR', 'nrdJ': 'Class II RNR', 'nrdD': 'Class III RNR'}
Common Issues
- Mixing up the order of items in the
zip
- Mixing up which list provides keys and which provides values
- In more complicated problems, overwriting existing dictionary keys
- Potential problems when zipping lists with different lengths
Optional Extensions
- Use a
for
loop, but without using thezip()
function to get key-value pairs - Use
zip()
, but don’t use afor
loop to build the dictionary
C.13 List Comprehension for Sequence Conversion
Task Description
Given dna = "ATGCTTAC"
, use a list comprehension to generate a list of its RNA bases (replace every "T"
with "U"
, others stay the same). Print the resulting list.
Learning Objectives
- Use list comprehensions for transformation
- Apply conditional logic inside a comprehension
Solution
# Write your code here!
Test Cases
# Output:
# ['A', 'U', 'G', 'C', 'U', 'U', 'A', 'C']
Common Issues
- Using
if base = "T"
instead of==
- Not understanding the position/order of
if
andelse
in the comprehension
Optional Extensions
- Join the resulting list into a string using
join()
C.14 Medium: Counting with defaultdict
Task Description
Given a DNA string sequence = "AAGCGAAGCT"
, use defaultdict
from the collections
module to count the occurrences of each base. Print the counts for each base.
Learning Objectives
- Import and use
defaultdict
- Count frequencies programmatically
Solution
# Write your code here!
Test Cases
# Something like:
# A => 4
# G => 3
# C => 2
# T => 1
Common Issues
- Forgetting to import
defaultdict
- Not using
counts[base] += 1
properly
Optional Extensions
- Try using a regular dictionary rather than a
defaultdict
- Try using a
Counter
instead of adefaultdict
C.15 Nested Dictionaries and Access
Task Description
Given the nested dictionary:
= {
gene_info "nrdA": {"product": "Class I RNR", "location": {"start": 1023, "end": 3268}}
}
Print the start and end position of "nrdA"
by accessing nested keys.
Learning Objectives
- Access values in nested dictionaries
Solution
# Write your code here!
Test Cases
# Output:
# start=1023
# end=3268
Common Issues
- Misplacing or omitting square brackets
- Wrong ordering of keys
Optional Extensions
- Try to print a value for a key that does not exist and observe the error
- Add another gene to the dictionary, and loop through start positions for all genes
C.16 Reverse Complement DNA Sequence
Task Description
Write code that, given a DNA sequence string (e.g., "AAAACCCGGT"
), prints its reverse complement as a string. (A <-> T, C <-> G). Use a dictionary for base pairing and a loop.
Learning Objectives
- Use dictionaries for mapping
- Loop through a string in reverse
- Concatenate strings to build a new sequence
Solution
# Write your code here!
Test Cases
# Input: AAAACCCGGT
# Output: ACCGGGTTTT
# Input: "AGTC"
# Output: "GACT"
Common Issues
- Incorrect complement dictionary
- Forgetting to reverse the DNA sequence
- In more realistic problems, improper handling of ambiguous bases (see IUPAC conventions)
Optional Extensions
- Write your solution using a list comprehension and the
join()
method - Don’t use a dictionary to store base complement info
C.17 Simulating Random Growth in a List
Task Description
Simulate a population of 10 bacteria, each with starting size 5. On each turn, every bacterium will grow or shrink according to these rules:
- On even numbered turns (0, 2, 4, …), bacteria with even indices grow by 1 unit, and the other bacteria stay the same
- On odd numbered turns (1, 3, 5, …), bacteria with even indices stay the same, and the other bacteria shrink by 1
Run the simulation for 10 turns, starting with turn 0
. At the start of each turn, print the bacteria list. Finally, when the simulation is over, print the final bacteria list.
Learning Objectives
- Practice with nested loops
- Update values in a list
- Check conditions for breaking loops
Solution
# Write your code here!
Test Cases
# Output:
# Turn 0: [5, 5, 5, ...]
# Turn 1: [...]
# ...
# Turn 9: [...]
# At the end: [...]
Common Issues
- 0-based indexing issues or forgetting 0 counts as even
- Mixing up conditional logic
- Forgetting how the modulo operator (
%
) works - Printing results at the wrong time in the loop (e.g., at the end rather than the start)
Optional Extensions
- Track and print the average population size at the end of each turn
- Change the rules to use the random module to make a more interesting simulation