If condition is met, skip from 2nd iteration onwards

Question:

#Can be more than two items also.
item_list = ["rpm", "exe"]

extraction_status = True
while extraction_status:
    file_status_flag = True
    
    file_status = some_other_function(arg1, arg2) # returns a nested dictionary of lists.
    
    for item in file_status['BU']: # file_status['BU'] = ["HR", "DEV" ,"Admin"]
        if item['extensions'] in item_list: #rpm or exe or zip
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
                continue # not working
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False       

Let me explain, if HR is of status PUSHED, for the 1st time or iteration, do certain actions, from subsequent iteration onwards, skip if condition is met.

continue skips the current iteration, which I’m not looking for.

Is it possible ?

Example output

1st iteration:
file from BU reached target
file from DEV In Progress
file from Admin Not Prepared

2nd iteration: 
file from DEV In Progress
file from Admin In Progress

so on..

Asked By: Chel MS

||

Answers:

Why not just add another flag to see if you already checked the condition? For example, we can add a flag called already_checked, set it to True on the first iteration and subsequently, if it’s already True then skip doing further actions:

item_list = ["rpm", "exe"]

extraction_status = True
while extraction_status:
    file_status_flag = True
    already_checked = False    # add this flag 
    
    file_status = some_other_function(arg1, arg2)
    
    for item in file_status['BU']: 
        if (item['extensions'] in item_list) and not already_checked:
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
                already_checked = True     # on the first successful iteration set the flag
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False 
Answered By: pavel

There is no magic I am aware to do that. But you can just use some extra variable:

#Can be more than two items also.
item_list = ["rpm", "exe"]

extraction_status = True
first_iteration = True

while extraction_status:
    file_status_flag = True
    
    file_status = some_other_function(arg1, arg2) # returns a nested dictionary of lists.
    # In case of next iterations we will skip first element. In case of bigger generators use itertools.islice 
    items = file_status['BU'][1:] if not_first_iteration else file_status['BU']
    for item in items: # file_status['BU'] = ["HR", "DEV" ,"Admin"]
        if item['extensions'] in item_list: #rpm or exe or zip
            if (file_status['status'] == "PUSHED")):
                print("file from {} reached target..".format(item))
                #do further action on that file
            else:
                print("Status of {} is {}".format(item, file_status['status']))
    # To check if all the items in item_list has been pushed.               
    if (file_status_flag and (file_status['status'] == "PUSHED")):
        "All files reached target..make the script successful.."
        extraction_status = False
    first_iteration = True
Answered By: kosciej16
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.