MarkS
MarkS

Reputation: 1539

Return a list with same number of elements

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().

Upvotes: 0

Views: 32

Answers (1)

Johannes
Johannes

Reputation: 3388

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.

Upvotes: 1

Related Questions