user15299034
user15299034

Reputation:

How to break out of a for loop using a function?

I'm running an if action defined in a function on a list of elements using a for loop. There is a secondary action within the first action. I want the for loop to stop once the action is successful the first time. Below is sample code to demonstrate:

my_list = [99, 101, 200, 5, 10, 20, 40]

def action(x):
    if x >= 100:
        print('It is finished')
        over_100 = True
        return over_100

def action2(x):
    x += 1
    action(x)
    
over_100 = False
for number in my_list:
    action2(number)
    if over_100:
        break

I want the for loop to stop at the first instance of >=100. For example, it should add 1 to 99 (first element of the list) and then stop everything. Instead, it is printing "It is finished" 3x because it loops through the whole list.

Upvotes: 2

Views: 189

Answers (4)

jkr
jkr

Reputation: 19310

You can make the function return a value and check the value in the loop. And you can use break to break out of the loop.

list_of_scopes = [scope1, scope2, scope3, etc.)

def action_function():
  return 'TEST' in xxxxx


for scope in list_of_scopes:
  found = action_function()
  if found:
    break

As of Python 3.8 you could even use the walrus operator which makes for more readable code:

for scope in list_of_scopes:
    if found := action_function():
        # optionally do something with `found`
        break

Upvotes: 9

Razzle Shazl
Razzle Shazl

Reputation: 1330

I think you are looking for global.

Global tells your function's scope to not create a new variable over_100 but instead re-use an over_100 that was declared in a higher scope.

I do prefer the other answers that return a value instead of polluting global scope. Whatever works!

my_list = [99, 101, 200, 5, 10, 20, 40]
over_100 = False
def action(x):
    global over_100
    if x >= 100:
        print('It is finished')
        over_100 = True

def action2(x):
    global over_100
    x += 1
    action(x)
    if over_100:
        return
    # here is more work for action2 that you skip when over_100
    
for number in my_list:
    action2(number)
    if over_100:
        break

Output:

It is finished

Upvotes: 0

DevLounge
DevLounge

Reputation: 8447

A totally different approach to show what I meant in my comment on the question:

from contexlib import suppress


class StopProcessing(StopIteration):
  pass


def action_function(param):
  if 'TEST' in param:
    print('TEST is found!')
    raise StopProcessing


with suppress(StopProcessing):
  for scope in list_of_scopes:
    action_function()

Upvotes: 2

inspectorG4dget
inspectorG4dget

Reputation: 114035

list_of_scopes = [scope1, scope2, scope3, etc.)

def action_function(param):
  found = False
  if 'TEST' in param:
    print('TEST is found!')
    found = True

  return found


for scope in list_of_scopes:
  result = action_function(scope)  # or whatever parameter
  if result:
    break

Upvotes: 0

Related Questions