Data Structures - Answer Key

Authors

Noor Sohail

Will Gammerdinger

Published

March 14, 2026

Exercise 1

  1. How would you access the fourth element of the species list and what is it?
# Access the fourth element of the species list
species[3]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[8], line 2
      1 # Access the fourth element of the species list
----> 2 species[3]

IndexError: list index out of range

We only have 3 elements in our species list, so trying to access the fourth element with species[3] will result in an error. If you try to access an index that is out of range for a list, you will get an IndexError. This is because the indices for our species list only go up to 2 (since we have 3 elements, and indexing starts at 0).

  1. Add "yeast" to the species list and print the updated list.
# Append yeast to the species list
species.append("yeast")
print(species)
['ecoli', 'human', 'corn', 'yeast']

Exercise 2

  1. How might you access the last two elements of the species list using slicing?

Hint: Recall that we can use negative indexing to access elements from the end of the list.

# Access the last two elements of the species list using slicing
species[-2:]
species[-2:len(species)]
['corn', 'yeast']
  1. You can actually slice with steps using the syntax list[start:stop:step]. This allows you to access every step-th element in the range from start to stop. How would you access every other element in the species list using slicing?
# Access every other element in the species list using slicing
print(species[::2])
print(species[0:4:2])
['ecoli', 'corn']
['ecoli', 'corn']

Exercise 3

  1. How would you access the genome length for corn in the genome_dict dictionary?
# Access the value for the key "corn"
genome_dict["corn"]
50000

Reuse

CC-BY-4.0