Search code examples
pythonlistloopsscheduleitems

it is possible to ignore the last element of the list?


I have 2 lists that have work hours and work to do

hours = ["00:00","06:00","08:30","09:30","10:30","12:30","14:30","16:00","18:00",]
jobs  = [job1,job2,job3,job4,job5,job6,job7,job8,job9]

with a loop to use the schedule library to schedule the jobs.

for i,x in zip(hours,jobs):
    schedule.every().monday.at(i).do(x)
    schedule.every().tuesday.at(i).do(x)
    schedule.every().wednesday.at(i).do(x)
    schedule.every().thursday.at(i).do(x)
    schedule.every().friday.at(i).do(x)

while True:
        schedule.run_pending()
        time.sleep(1)

what I want to do is that on Fridays schedule.every().friday.at(i).do(x) I schedule all the jobs except the last one

job9 at 18:00

Is there any way to avoid the last item in the list in that case?


Solution

  • I believe Ismail's answer will avoid scheduling the last job on ALL days. If you want only Friday to be affected place the if statement before Friday:

    for i,x in zip(hours,jobs):
        schedule.every().monday.at(i).do(x)
        schedule.every().tuesday.at(i).do(x)
        schedule.every().wednesday.at(i).do(x)
        schedule.every().thursday.at(i).do(x)
        if i != hours[-1]:
            schedule.every().friday.at(i).do(x)