0

I want to write a line of code that checks if the iteration i==someValue and the next iteration i==anotherValue then write a line of code in the loop.

E.g

for line in file:
    if line[1]=='A' and if next(line[1]) == 'B'
    print("do something here")

How can I go about achieving this?

Thanks!

Jonas
  • 121,568
  • 97
  • 310
  • 388
Mike Issa
  • 295
  • 2
  • 13
  • 2
    Possible duplicate of [Iterate a list as pair (current, next) in Python](http://stackoverflow.com/questions/5434891/iterate-a-list-as-pair-current-next-in-python) – Mel Dec 15 '15 at 20:28

1 Answers1

1

One solution is to store the value that you want for the first line as a control value. If the next line does not have the correct value - the control value is cleared. For example like:

file = ['A', 'A', 'B', 'C', 'A', 'C', 'B']
control_value = ''
for line in file:
    if line == 'A':
        control_value = line
        continue
    if line == 'B' and control_value == 'A':
        print('Do something here')
    control_value = ''

Any values except 'A' will reset the control_value.

oystein-hr
  • 551
  • 4
  • 9