Context: I have sublists inside the main list called lst_separated
and I'd like to go through each element of the sublist using a while loop, but I can't seem to figure how to. I also have a second list that has the indices for each sublist inside the lst_separated
called populated_indices
. The goal is to pick a sublist (let's say this one: ['230320','230400','25020','26232','302320,312320]
), go through each pair (so i and i+1 elements - For example '230320' (even index) and '230400'(odd index) would be a pair, '25020' and '26232'would be another pair, etc) and put these positions as integers in the variable var
.
Code:
lst_separated = [['1605',1607],['230320','230400','25020','26232'],['230320','230400','25020','26232','302320','312320'],...]
populated_indices = [17,430,678...]
outcome = []
for seqName,index in zip(pca_df['seqName'], range(0,len(pca_df))):
i = 0
j = 1
while j < len(lst_separated[populated_indices[index]]):
pos1 = int(lst_separated[populated_indices[index]][i])
i += 2
pos2 = int(lst_separated[populated_indices[index]][j])
j += 2
#print(pos1,pos2)
var = str(record_dict_2[seqName].seq)[pos1-1:pos2-1]
outcome.append(var)
#print(lst_separated[subindex])
#print(populated_indices)
I can get the positions when each sublist is 2 elements long, but I eventually reach a IndexError: list index out of range in the while loop and I don't think it is looping through every element of each sublist as I want it to (only the first 2 elements even if it has 4 or 6 elements for example). Is there a better way to do this? Possibly with itertools?
Thank you in advance!