How to call the next line during enumerate when using if-and-if statement

221 Views Asked by At

I am working on the code below where AnalyteName_user_input is a list of strings as seen in the image below.

AnalyteName_user_input

Occasionally there is an instance where the value of index seven is 'W' rather than 'W2'. The current goal is to create a new list which would be AnalyteRatio_user_input. This would contain the values of a ratio evaluation between float values from the MeanResponse_list list. I believe the current issue lies in line 3 where I am attempting to use an if-in-and-if-in statement. I believe the issue is that I am trying to call the next line in the enumeration but line+1 is the 'W' value from the AnalyteName_user_input list. How would I remedy this to call the next line in the list rather than the string and then adding some int value?

AnalyteRatio_user_input = []
for i, line in enumerate(AnalyteName_user_input):
    if 'W' in line and 'W2' in line+1: # need a way to define the next line. Unsure about this.
    AnalyteRatio_user_input.append(MeanResponse_list[i]/MeanResponse_list[i+5])
    AnalyteRatio_user_input.append(MeanResponse_list[i+1]/MeanResponse_list[i+5])
    AnalyteRatio_user_input.append(MeanResponse_list[i+2]/MeanResponse_list[i+5])
    AnalyteRatio_user_input.append(MeanResponse_list[i+3]/MeanResponse_list[i+5])
    AnalyteRatio_user_input.append(MeanResponse_list[i+4]/MeanResponse_list[i+5])
    AnalyteRatio_user_input.append(MeanResponse_list[i+5]/MeanResponse_list[i+5])
else:
    AnalyteRatio_user_input.append('N/A') 
1

There are 1 best solutions below

1
t.m.adam On BEST ANSWER

Try this line

if 'W' in line and ('W2' in AnalyteName_user_input[i+1] if i < len(AnalyteName_user_input)-1 else True) : 

Note that the second condition allways evaluates to true for the last line .
If you don't want that you should change True to False