0

I'm trying to find a way to print the index of each 'A' in this DNA string.

Tried:

dna = 'ATCACGACGTGACGGATCGAGCAGTACG'

for nuc in dna:

    if nuc == 'A':
        print (dna.index(nuc))

Output:

0
0
0
0
0
0
0
0

Which I understand is because the index of the first 'A' is 0. What I'm trying to figure out is if it's possible to use a for loop to print the index of each individual 'A' in the string (so I know where each 'A' is in the string).

2 Answers 2

3

One simple way is to use enumerate:

dna = 'ATCACGACGTGACGGATCGAGCAGTACG'

output = [i for i, x in enumerate(dna) if x == 'A']
print(output) # [0, 3, 6, 11, 15, 19, 22, 25]
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! that's exactly what I was trying to do. thanks so much!!
1

What is happening is that when your loop encounters an 'A' you call dna.index which will always return the first instance of the character in the sequence when called without the additional start parameter.

A work around would be to keep track of the index your loop is currently at and print that when you encounter the matching character.

For that you can use the range, or enumerate function:

for i in range(len(dna)):
    if dna[i] == "A":
        print(i)

or

for i, nuc in enumerate(dna):
    if nuc == "A":
        print(i)

The second example is essentially the same as j1-lee's answer, without the list comprehension. For the sake of brevity and readability I would always recommend using list comprehension like in j1-lees answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.