-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNested_Dictionary.py
More file actions
36 lines (32 loc) · 1.22 KB
/
Nested_Dictionary.py
File metadata and controls
36 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Access the elements in a nested dictionary
# Method 1:
amino_acid_galleries = {
'Cysteine': {
'Formula': 'HOOC−CH(−NH2)−CH2−SH',
'Function': 'Help prevent side effects due to drug reactions and toxic chemicals'
},
'Serine': {
'Formula': 'HO2C−CH(NH2)−CH2OH',
'Function': 'Involved in the synthesis of purines and pyrimidines'
},
'Lysine': {
'Formula': 'H2N−(CH2)4−CH(NH2)−COOH',
'Function': 'Used in the biosynthesis of proteins'
}
}
amino_acid_galleries['Cysteine']['Formula']
# 'HOOC−CH(−NH2)−CH2−SH'
# Iterate over a nested dictionary
for amino_acid, values in amino_acid_galleries.items():
print(f"The function of {amino_acid.title()} is {values['Function']}.")
# Method 2:
print(amino_acid_galleries['Cysteine'].keys())
# dict_keys(['Formula', 'Function'])
amino_acid_galleries['Cysteine'].get('Formula')
# 'HOOC−CH(−NH2)−CH2−SH'
for name in amino_acid_galleries:
print(name, amino_acid_galleries[name].get('Formula', 'N/A'))
# Cysteine HOOC−CH(−NH2)−CH2−SH
# Show all the amino acids that have the formulas
print([name for name in amino_acid_galleries if 'Formula' in amino_acid_galleries[name]])
# ['Cysteine']