The genetic code: the codon table as a dictionary
The genetic code is the master lookup table every living thing shares: which codon means which amino acid. In Python, it's naturally a dictionary — and exploring it is just querying that dict.
The big idea
The genetic code maps all 64 codons to amino acids (or a stop signal), with AUG as start and three stop codons.
See it in code
The genetic code is a lookup: give it a codon, get back an amino acid. In Python that's a dictionary, and reading it is a single square-bracket lookup by key:
table = {"AUG": "Met", "GCU": "Ala", "UAA": "Stop"}
print("AUG codes for:", table["AUG"])
print("GCU codes for:", table["GCU"])AUG codes for: Met GCU codes for: Ala
table["AUG"] finds Met instantly — no scanning, no ifs. That's the dictionary's whole point: name the key, get the value.
Loop over .items() to read the whole table at once. Watch what happens with GCU and GCC — two different codons, one amino acid:
table = {"AUG": "Met", "GCU": "Ala", "GCC": "Ala", "UAA": "Stop"}
for codon, amino in table.items():
print(codon, "=", amino)AUG = Met GCU = Ala GCC = Ala UAA = Stop
GCU and GCC both land on Ala — the code is redundant. The real table takes that much further.
Here's a slice of the real table. Notice several codons can map to the same amino acid (the code is redundant), that AUG doubles as the start signal, and that three different codons all mean Stop:
GENETIC_CODE = {
"AUG": "Met (start)",
"GCU": "Ala", "GCC": "Ala",
"UAA": "Stop", "UAG": "Stop", "UGA": "Stop",
}
print("AUG is the", GENETIC_CODE["AUG"], "codon")
print("Stop codons:", [c for c, a in GENETIC_CODE.items() if a == "Stop"])
print("Total codons in this table:", len(GENETIC_CODE))AUG is the Met (start) codon Stop codons: ['UAA', 'UAG', 'UGA'] Total codons in this table: 6
GCU and GCC both map to Ala — that redundancy protects against some mutations. Filtering .items() found all three stop codons in one line. The full code has 64 entries; the structure is identical, just longer.
A shared lookup standard is a powerful idea: ASCII maps numbers to characters, HTTP maps status codes to meanings, currency codes map symbols to countries. The genetic code is nature's version — one table, read the same way in every cell on Earth.
Try it yourself
Add more real codons to the table and translate a strand. Then invert it: build a dictionary from amino acid back to its codons, and notice most amino acids have several.
The common mistake
Assuming one amino acid means one codon. The mapping is many-to-one: up to six codons can code for the same amino acid. Going codon-to-amino-acid is a clean lookup; going backward gives you a list of possibilities.
What it unlocks
The code is the reference for codons, translation, and proteins — all powered by dictionaries.