Dictionaries
Overview
Teaching: 15 min
Exercises: 15 minQuestions
What is a dictionary, and how do I use it?
Objectives
Explain how dictionaries work.
Learn about dictionary operations.
A dictionary allows you to keep data associated to a custom key
- Different from lists
- You access elements in lists by their position, but in a dictionary the keys don’t need to be ordered.
- Different from sets
- Sets contain only unique values with no associated keys, while dictionaries map keys to values.
- A very fast data structure
- Lookups, insertions, and deletions in dictionaries are very fast (average-case O(1)).
Basic Operations
- Creation: Use curly braces
{}or thedict()constructor. - Access: Use square brackets (
dict[key]) or theget()method. - Modification: Add or update entries using assignment (
dict[key] = value). - Deletion: Use the
delstatement or thepop()method. - Keys: Use
dict.keys()to get all keys in the dictionary. - Values: Use
dict.values()to get all values in the dictionary.
# A simple example of a dictionary
# The keys can be strings or numbers (ints or floats)
# With the values in the dictionary you have a bit more freedom
data = {'a': "Hello", 'b': 2, 'c': ["apple", "banana", "cherry"]}
print(len(data)) # Output: 3
print(data['a']) # Output: Hello
Example: DNA Codon Table
Consider a simplified DNA codon table where each codon (a triplet of nucleotides) maps to an amino acid.
# Example input here
codon_to_amino = {
"ATG": "Methionine",
"TTT": "Phenylalanine",
"TTC": "Phenylalanine",
"TAA": "Stop",
"TAG": "Stop",
"TGA": "Stop"
}
# Access amino acid for codon 'ATG'
print("Codon ATG codes for:", codon_to_amino["ATG"])
Codon ATG codes for: Methionine
Initialising
What does the following program print?
codon_dict = {"ATG": "Methionine", "TAA": "Stop", "TAG": "Stop"} codon_dict["ATG"] = "Start" # Change: ATG now maps to Start codon_dict["TGA"] = "Stop" print("Updated codon dictionary:", codon_dict)Hint: The order of keys may vary.
Solution
The program prints a dictionary with keys `'ATG'`, `'TAA'`, `'TAG'`, and `'TGA'`. The value for `'ATG'` is updated to `"Start"`. For example: ~~~python Updated codon dictionary: {'ATG': 'Start', 'TAA': 'Stop', 'TAG': 'Stop', 'TGA': 'Stop'} ~~~Fill in the Blanks
Fill in the blanks so that the program below retrieves the correct amino acid for the given codon.
codon_translation = {"GGT": "Glycine", "GGC": "Glycine", "GGA": "Glycine", "GGG": "Glycine"} amino_acid = codon_translation[______] print("Amino acid for GGT:", amino_acid)Expected output: Amino acid for GGT: Glycine
Solution
Replace the blank with `"GGT"`: ~~~python amino_acid = codon_translation["GGT"] ~~~Adding a New Codon
Extend the dictionary by adding the codon
"CCC"for"Proline"to the dictionary below.codon_dict = {"ATG": "Methionine", "TAA": "Stop"} # Add code here print(codon_dict)Expected output: {‘ATG’: ‘Methionine’, ‘TAA’: ‘Stop’, ‘CCC’: ‘Proline’}
Solution
~~~python codon_dict["CCC"] = "Proline" ~~~Key Points
A dictionary stores values accessible by unique keys.
Dictionaries may contain values of different types.