0

i'm struggling to find the preceding item from a list based on a variable. Let say i have a list

date = ['20190501','20190502','20190503','20190507','20190508'] 

and i have another variable stored as:

start_date = ['20190507']

what i would like to find is the preceding value of the start_date and store it as previous_date which i'm calling further down in my code.

So in this case, the previous_date would be ['20190503'].

In another case if my start_date = ['20190503'] and the list is the same, the previous_date would be ['20190502'].

1 Answers1

0

You have a List structure so you can use its built-in methods like index. It returns a 0-based index of the first item equal to its x argument, something like:

%py
date = ['20190501','20190502','20190503','20190507','20190508'] 

start_date = ['20190507']

## So in this case, the previous_date would be ['20190503'].
x = ['20190501','20190502','20190503','20190507','20190508'].index(start_date[0])

date[x - 1]

My results:

My results

wBob
  • 13,710
  • 3
  • 20
  • 37