0

I have a program that converts DNA sequences into RNA sequences.

The translation part works fine. Where I am stuck is that I input a list of four elements, but I am getting back a list with a single element back.

My code:

dnasequences = [
    'GCTAGCTAGCTAGCTA',
    'CTAGCTAGCTAGCTAG',
    'TAGCTAGCTAGCTAGC',
    'AGCTAGCTAGCTAGCT'
]

xlate = {'G': 'C', 'C': 'G', 'T': 'A', 'A': 'U'}


def dna2rna(sequences):
    rnalist = [xlate[n] for sequence in sequences for n in sequence]
    return rnalist

rnasequences = dna2rna(dnasequences)
print(''.join(rnasequences))

This returns:

CGAUCGAUCGAUCGAUGAUCGAUCGAUCGAUCAUCGAUCGAUCGAUCGUCGAUCGAUCGAUCGA

The translation is correct, but I want rnasequences() to contain four 16-character elements just like the input list dnasequences().

MarkS
  • 1,455
  • 2
  • 21
  • 36

1 Answers1

1

Currently your list rnasequences contains 64 elements of a single character. You can split this list into smaller lists of 16 elements and join them, that way you get strings of length 16:

>>>[''.join(rnasequences[i:i+16]) for i in range(0, len(rnasequences), 16)]
['CGAUCGAUCGAUCGAU',
 'GAUCGAUCGAUCGAUC',
 'AUCGAUCGAUCGAUCG',
 'UCGAUCGAUCGAUCGA']

To understand how the splitting works have a look at this question.

Johannes
  • 3,300
  • 2
  • 20
  • 35
  • Your solution worked perfectly, and I reviewed the link you supplied. It occurs to me that this solution works because all of the inputs are of equal length. If that were not the case then this solution would not work. Is there a way to match, on an element by element basis, the lengths of the list elements in the input list to the output list? – MarkS Jun 30 '17 at 14:54