TransWikia.com

handling for loop exceptions

Stack Overflow Asked by Sivaram on December 25, 2021

a = 6
item_list = [1,2,3,4,5]
for items in item_list:
    if items == a:
        print("match found")
    else:
        print ("no match")

In this code I want "no match" to be printed only once after completing all the iterations; not on every iteration. How can I modify the code?

6 Answers

You could use a while loop:

a = 6
item_list = [1, 2, 3, 4, 5]
i = 0
found = False
while True:
    i += 1
    if item_list[i] == 6:
        print("Found")
        found = True
    if i > len(item_list):
        break
if not found:
    print("Not found")

Note that 6 is not in the list, so it will print "Not found"

Answered by Psychzander on December 25, 2021

As others have pointed out, the most efficient solution in this situation is to use an in test, and no explicit loop is needed.

However, in a more general situation where one is looping until a match is found (if more complex logic means that there is no simple equivalent of in available), it is worth remembering that a for loop can have an else block which is run after the loop completes if it is has not been exited using break. Taking this code as an example (even though not necessary in this situation), you could do:

a = 6
item_list = [1,2,3,4,5]
for item in item_list:
    if item == a:
        print("match found")
        break
else:
    print ("no match")

Note that this is not exactly equivalent to the code in the question if more than one match is found, as here "match found" is only printed once and then the loop is exited via break. If you were looking for potentially more than one match, for example saving these to a list, then you would not use break and else, but would instead test afterwards whether your output list was empty before printing "no match".

Answered by alani on December 25, 2021

a = 6
item_list = [1,2,3,4,5]
found_in_list_boolean = False

for items in item_list:
    if items == a:
        print("match found")
        '''Set the boolean to True as you reached the match found statement '''
        found_in_list_boolean = True

'''2 ways from here'''

'''First Way : If found_in_list_boolean is true print something and for false print not found'''
if (found_in_list_boolean):
    print("Found")
else:
    print("not found")


'''Second Way :If found_in_list_boolean is false and print'''
if (found_in_list_boolean==False):
    print("not Found")

Answered by Rahul Shyokand on December 25, 2021

a in b is generally a better solution, but if you want to loop through the entire list you could create a variable to store matches in and check if the variable is populated when the for-loop expires.

a = 6
memory = []
item_list = [1,2,3,4,5]
for items in item_list:
    if items == a:
         memory.append(a)
         print("match found")
if len(memory) == 0:
    print("No matches")

a is never gonna show up in the loop since you're only looping over elements in item_list, fyi

Answered by NotActuallyErik on December 25, 2021

Use instead:

a = 6
item_list = [1,2,3,4,5]
if a in item_list:
    print("match found")
else:
    print ("no match")

No need to iterate list by yourself, use built-in method.

Answered by ipj on December 25, 2021

Instead of looping through, you can use index on list or using in operator

In [39]: a = 6
    ...: item_list = [1,2,3,4,5]

In [40]: try:
    ...:     index = item_list.index(a)
    ...:     print("match found")
    ...: except:
    ...:     print("no match")
    ...:
no match

In [41]: if a in item_list:
    ...:     print("match found")
    ...: else:
    ...:     print("no match")
    ...:
no match

Answered by bigbounty on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP