But as you just learned, there are 20 different amino acids. So if we wanted to store all 20 amino acids as variables, we would have to create 20 different variables (e.g., my_amino_acid_1, my_amino_acid_2, ..., my_amino_acid_20). An easier way to store many valuables is, quite intuitively, in a list. A list is simply a changable, ordered sequence of elements. Just like strings are defined as ordered characters between quotes "", lists are defined as comma-delimited elements between square brackets []. (Lists can also store any data type or combination of data types — for example, strings and floats can be in a single list together.) We can then store all 20 amino acids as a list:
1 # Store all 20 amino acids in a list
2 amino_acids = ['alanine', 'arginine', 'asparagine', 'aspartic acid', 'cysteine', 'glutamic acid', 'glutamine', 'glycine', 'histidine', 'isoleucine', 'leucine', 'lysine', 'methionine', 'phenylalanine', 'proline', 'serine', 'threonine', 'tryptophan', 'tyrosine', 'valine']
3 print(amino_acids)
Try it yourself here!
Let's discover some other things about lists. Type the following into the terminal and see what happens!
1 amino_acids = ['alanine', 'arginine', 'asparagine', 'aspartic acid', 'cysteine', 'glutamic acid', 'glutamine', 'glycine', 'histidine', 'isoleucine', 'leucine', 'lysine', 'methionine', 'phenylalanine', 'proline', 'serine', 'threonine', 'tryptophan', 'tyrosine', 'valine']
2
3 print(amino_acids[0])
4
5 print(len(amino_acids))
We have said that a list is an ordered sequence of elements. This ordering is useful, in that we can access the first, fifth, or last element in the list very quickly as well as determine the length of the list.
The number in the list that an element occurs is called that element's index. To print any element in a list, use the syntax my_list[ index ]. But be careful: Python lists are zero-indexed! That is, the first element in the list has index 0, and the fifth element in the list has index 4. So, to print the first amino acid in our protein and the length of that list:
1 print(amino_acids[0])
2 print(len(amino_acids))
Print the last amino acid in the following amino acid list WITHOUT COUNTING! Hint: remember that the list is zero-indexed and that the len() formula gives you the total length of the list
1 my_protein = ['methionine','valine','leucine','serine', 'proline', 'alanine', 'lysine', 'threonine','asparagine', 'valine','lysine', 'tryptophan', 'glycine','lysine', 'valine', 'glycine','alanine']