Write dict to list, remove duplicates and rename single enties of the list- only python

Viewed 36

gencode = { 'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M', 'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T', 'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K', 'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R', 'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L', 'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P', 'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q', 'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R', 'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V', 'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A', 'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E', 'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G', 'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S', 'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L', 'TAC':'Y', 'TAT':'Y', 'TAA':'', 'TAG':'', 'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'}

a) Write Python code that generates a list of the one-letter codes given in the dictionary gencode and assigns them to the variable aas variable. The list should contain each AS only once and store stop codons as "*".

(b) Sort the list aas alphabetically.

c) Write Python code that displays the associated codons for each AS in the list aas. For each AS, output a line of the form "A: nnn mmm", where A is the AS, and nnn, mmm, ... the associated codons.

What I tried so far:

gencode = {
'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'}
aas = []
for val in gencode.values():
    aas.append([val])
    aas.sort()
    aas1 = []
    for i in aas:
        if i not in aas1:
            aas1.append(i)
    a = str(aas1)
    a.replace('[_]', '*')
print(a)

but replace doesn´t work for lists and for some reason does not work after converting to str either, maybe I shoukd try another way but I don´t know which.

Thanks for any input! BR RThunder

1 Answers

(a) Take the values of gencode dictionary. set eliminates duplicates. Then make a list.

aas: list[str] = list(set(gencode.values()))
if '_' in aas:
    aas[aas.index('_')] = '*'
print(aas)

(b) Sort the list.

aas.sort()
print(aas)

Output:

['*', 'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']

(c) “Transpose” the dictionary.

a: str
for a in aas:
    codons: set[str] = set(code 
                           for code in gencode 
                           # the complex condition reverses the change “*” <-> “_”
                           if (gencode[code] == (a if a != '*' else '_')))
    print(f'{a}:', ' '.join(codons))

Output:

*: TAA TGA TAG
A: GCG GCT GCC GCA
C: TGC TGT
D: GAT GAC
E: GAG GAA
F: TTT TTC
G: GGG GGC GGA GGT
H: CAC CAT
I: ATC ATT ATA
K: AAA AAG
L: TTA CTC CTG TTG CTT CTA
M: ATG
N: AAC AAT
P: CCG CCT CCC CCA
Q: CAA CAG
R: CGA CGT CGC AGA CGG AGG
S: AGC TCA TCC AGT TCG TCT
T: ACA ACC ACG ACT
V: GTA GTG GTC GTT
W: TGG
Y: TAT TAC
Related