Genetics Refresher
Lists & Loops Refresher
List Methods
Dictionaries
Checkpoint
Build Your Own Protein
Summary
Dictionaries
Let's talk about a new type of data structure. Think about a dictionary. Yes, a real, paper, heavy, old, musty dictionary (try to imagine the days before Google!). In the olden days, we had to look up the definition of a word we didn't understand in a dictionary. Every word in the dictionary is unique and associated with a definition. And it was actually pretty easy to use a dictionary; you could find the definition that you were looking for really fast (maybe not quite as fast as Google)! Just like paper dictionaries, Python has a data structure that works just like a dictionary - you can use a key (like a word you don't understand) to get information associated with that key (a value, like a definition of the word) really quickly. And it's even called a dictionary in Python!
A couple of important things to know about Python dictionaries:
-
Dictionaries are unordered key-value pairs. This is where Python dictionaries differ from paper dictionaries; while a paper dictionary is in alphabetical order, Python dictionaries are not ordered at all. This means we can't use indexing, like in lists, to access the values of a dictionary (we have to use the keys!). But that doesn't mean that it's not fast to look up the value associated with a key!
-
Dictionary keys all must be unique (think about all of the confusion that homonyms already cause for paper dictionaries!). But the values that they are associated with don't have to be unique (just like synonyms!).
-
Dictionaries are especially useful when trying to associate one string with another (although keys and values can be other data types as well, including strings or integers).
-
Dictionaries are not the same as defining a bunch of new variables! A variable name cannot be a string and a string cannot be a variable name. However, a dictionary key can be a string. Moreover, keys only access their paired value when called in the dictionary.
Remember the Genetic Code? A dictionary is the perfect data structure to store the Genetic Code, because we have unique keys (triplets of nucleotides, like ATG) that code for not-necessarily-unique values (amino acids, like methionine). We can create the start of this Genetic Code dictionary with this syntax:
1 genetic_code_dict = { 'ATG': 'methionine', 'TTT': 'phenylalanine', 'CGG': 'arginine' }
2
3 print(genetic_code_dict['TTT'])
Notice the new syntax to create a dictionary. We can store the entire dictionary in a variable, here called genetic_code_dict. We set this variable equal to a dictionary, denoted by the curly brackets {}. Then, we can define each key : value pair, using a colon : between them and a comma , to separate the pairs.
Sometimes the dictionaries we create can get pretty long, so it would be easy to forget what keys and values are included in the dictionary. Luckily, it is easy to print out the keys and values of a dictionary as a list:
1 genetic_code_dict = { 'ATG': 'methionine', 'TTT': 'phenylalanine', 'CGG': 'arginine' }
2
3 print(genetic_code_dict.keys()) # Prints out keys of dictionary
4
5 print(genetic_code_dict.values()) # Prints out values of dictionary
6
7 print(len(genetic_code_dict))
Now that we've created a dictionary with key : value pairs, we need to know how to "read" the dictionary by looking up values by their keys. This is accomplished easily with the syntax dictionary[key]!