2

I am new to Python and am trying to figure out how to address the following issue. I have two lists, list1 looks like this:

['10','12','12','23]

And list2 looks like this:

['15','16','13','15']

I also have a dictionary consisting of the following key-values:

dictionary = {
'10': 'AATGCGTAAGCTAGCGATAGATGCAACGTT', 
'12': 'GGTCAGAGTTTATCGGTATGTTAGCATGGA', 
'23': 'ATGGGCGCGTATATTAAAGGCGCCGCCCTG'
}

The keys in this dictionary consist of those elements in list1. Each element in list1 is also associated with a single element in list2 (in order, such that '10' in list1 is associated with '15' in list2, '12' in list1 with '16' in list2, etc.).

For each element in list1, I would like to take the associated dictionary value, and insert characters into specific positions using the associated element in list2 to determine the positions. So far I've been working on a for loop to do this:

for i in list1:
    dict_key = dictionary.get(i)
    pos = int(list2[0])
    full = dict_key[:pos] + '[' + dict_key[pos] + ']' +  dict_key[(pos+1):]
    print(full)

I would like my final output to look like this:

AATGCGTAAGCTAGC[G]ATAGATGCAACGTT
GGTCAGAGTTTATCGG[T]ATGTTAGCATGGA
GGTCAGAGTTTAT[C]GGTATGTTAGCATGGA
ATGGGCGCGTATATT[A]AAGGCGCCGCCCTG

But my output currently looks like this because I am specifically calling for only index 0 from list2:

AATGCGTAAGCTAGC[G]ATAGATGCAACGTT
GGTCAGAGTTTATCG[G]TATGTTAGCATGGA
GGTCAGAGTTTATCG[G]TATGTTAGCATGGA
ATGGGCGCGTATATT[A]AAGGCGCCGCCCTG

I want to increase the element that's called for in list2 by 1 with each iteration of the loop, but I can't seem to figure it out. Should I define a new variable using range? Any comments will be appreciated!

3 Answers 3

2

Just change for i in list1: to for i, pos in zip(list1, list2): and remove the assignment of pos in the second line of the loop. zip will let you iterate through both lists at once.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this solution @Randy C! Very simple and does what I wanted, will also be helpful to know in general. Thanks again!
1

Using enumerate is helpful in situations like this - it gives you a tuple containing the position of each element, and the element itself. Note, I've changed your meaning of i to mean the element's position rather than the element.

Your variable dict_key would probably be better called dict_value, since the element in list1 is actually the key that you are looking up in dictionary.

for i, el in enumerate(list1):
    dict_value = dictionary.get(el)
    pos = int(list2[i])
    full = dict_value[:pos] + '[' + dict_value[pos] + ']' +  dict_value[(pos+1):]
    print(full)

1 Comment

Also very helpful, thanks @enigma, I had not previously used enumerate.
0

You can build iterators for your lists (int(i) for i in list2 for instance iterates list2, converting its values to int on the way) and use zip to read them in pairs.

list1 = ['10','12','12','23']
list2 = ['15','16','13','15']

dictionary = {
'10': 'AATGCGTAAGCTAGCGATAGATGCAACGTT', 
'12': 'GGTCAGAGTTTATCGGTATGTTAGCATGGA', 
'23': 'ATGGGCGCGTATATTAAAGGCGCCGCCCTG'
}

for val, pos in zip((dictionary[j] for j in list1), (int(i) for i in list2)):
    full = val[:pos] + '[' + val[pos] + ']' +  val[(pos+1):]
    print(full)

Some people would prefer string formatting to build the resulting string

full = '{}[{}]{}'.format(val[:pos], val[pos], val[pos+1:]) 

but that's personal taste.

1 Comment

Thanks @tdelaney, gives me what I want and I now know about list iterators.

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.