In C++, it’s possible to say:
for (int i = 0; i < 100 && !found; i++) {
if (items[i] == "the one I'm looking for")
found = true;
}
so you don’t need to use the “break” statement.
In Python, I guess you need to write:
found = False
for item in items:
if item == "the one I'm looking for"
found = True
break
I know that I can write a generator which has the same code in it so I can hide this break thing. But I wonder if there is any other way to implement the same thing (with the same performance) without using extra variables or while loop.
I know we can say:
found = "the one I'm looking for" in items
I’m just trying to learn if it’s possible to use multiple conditions in for loops.
Thanks.
Of course you can also write this without the lambda function as
Now it looks to me that it is the C version using extra variables
If you really just need the found variable set (and don’t need to know the item), then simply use