4

I have the following code

for x in list:
    if x.getValue == variableValue: 
        Print('Found')

I want to print a statement that says "no matches found" on the last iteration of the loop.

Is there anyway to know if the x currently being run through the for loop is the last x in the list?

3
  • What does 'not leaving the for loop' mean? Is it that you don't have an option to break because there's more than one matching item possible? Commented Apr 14, 2013 at 17:25
  • Sorry for the poor wording. I guess I wanted to run through the list and print 'Not Found' if no matches were found but during the last cycle of the for loop. I guess what I'm really asking if there is anyway to find if 'x' is the last 'x' in the list. Commented Apr 14, 2013 at 17:31
  • 1
    That is a different question to the one you asked. It's impossible to know that, as iterators don't have that information. That said, the value in a loop isn't constrained to the loop, so x at the end of the loop will always be the last value. Commented Apr 14, 2013 at 17:35

2 Answers 2

11
for x in list:
    if x.getValue == variableValue: 
        print('Found')
        break
else:
    print('not found')

This for else also works with while else where the else clause runs only if the iterable (e.g. list) is exhausted and no break has happened.

There are many who find this construct confusing to read, myself included.

Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't the 'break' cause the program to jump out of the loop?
About it being confusing: I look at it as if it was inside a function, instead of break we would use return and after the cycle we would return the 'else-result'. Makes sense?
-1, The OP makes it clear that the loop needs to continue even if a value is found.
@Lattyware Indeed, the question is ambiguous as there is no way to conclude "not found" until the iterable is exhausted.
Again, sorry for the poor wording. I edited the question. I was just wondering if there is any way to tell if an 'x' is the last 'x' in the 'list'.
8

Edit:

The updated question has a simple answer, no. Arbitrary iterators have no idea if they are on their last item, so there is no way for the for loop to know.

That said, the value in a loop isn't constrained to the loop, so x directly after the end of the loop will always be the last value.


If you wish to continue looping, you can simply set a flag:

found = False
for x in some_list:
    if x.value == value: 
        print('Found')
        found = True

if not found:
    print("Not Found.")

If you didn't want to do something on each step of the loop, you can use any() and a generator expression to find out there are no matches easily:

if not any(x.value == value for x in some_list):
    print("Not Found.")

6 Comments

@rmp2150, useful, fine, but does it answer your question?
@unkulunkulu You have just completely changed the question being asked.
@Lattyware, I changed it to what seems to be the intended question of the OP based on his comments and edit. If you have strong opinion that my edit is incorrect, feel free to rollback.
@unkulunkulu framed my question better. Sorry for the confusion
@unkulunkulu I get that, it just seems off to edit the question, and then instantly post a comment saying my answer isn't a relevant one, when I had not had the chance to update it. At the very least, it might have been worth saying 'this only answers the original question' so as not to make it sound like I had intentionally posted an irrelevant answer. In general, I would argue if the original question wasn't right, once answers have been posted, a new question should be asked instead.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.