Nice trick, but maybe cleaner if you embed the nested loops in a function and just use return
@pawlox1300 Жыл бұрын
true
@piotrmazgaj Жыл бұрын
Nah, it's fine.
@jumper0122 Жыл бұрын
Especially if you have more levels. I thought he was going to show a 3-level version of it to segway into using functions for this purpose, but he didn't get there
@k.chriscaldwell4141 Жыл бұрын
Yeah, that’s how I do it.
@DmitryRomanov Жыл бұрын
Or we may try/except and raise the exception. Or construct specific `with` for the purpose.
@the_huge_knight Жыл бұрын
I can create a function with that nested loops and when I want to break them out - simply write "return" or return an error, a value, etc.
@jvsonyt Жыл бұрын
This is great. Couldn't be more perfect timing for optimizing my current project
@loc4725 Жыл бұрын
Abusing the Exception system as some have suggested is generally considered bad coding practice. If you only have one nested loop then else / break is fine, otherwise in the absence of a global break or goto wrap it in a function call.
@vietndk5437 Жыл бұрын
Your explanation is helping my mind explore more about for loop and its application. Many thanks.
@rupen42 Жыл бұрын
It's a great feature to know about but I've often heard that you _shouldn't_ use it, specially in professional contexts. Guido even regrets adding it to the language. If you're in a team, some people might not be very familiar with that construct, so it's best to keep it simple (found = False ... if found_condition: found = True ... if found: break). It's only one boolean and not too many extra lines, a small price for better readibility. If you do use it in a group context, it's good practice to add a comment. (for ... else: # no break) Of course, it'll vary according to style guides, but I just wanted to push back against your claim that the boolean flag method shouldn't be used. It's at least more complicated than that.
@cestlacroix Жыл бұрын
single boolean variable can be performance overhead in some cases. hence this can be efficient when you have lots of iterations but simple. e.g.: socket reading, encoding/decoding.
@Snakyy1 Жыл бұрын
I think that’s a problem for your team. You shouldn’t not do something the correct way because someone might not know how it works
@cookiebinary Жыл бұрын
PHP (and some other languages) just add the number of cycles to break: `break 2` (that is, breaks 2 cycles)
@aouerfelli Жыл бұрын
If you apply this method when you have two loops inside the outer loop, you would be forced to put the second inner loop inside the "else" of the first inner loop. What about if you had three inner loops? It would be even more complicated. What about you have two types of "break" inside the inner loop? one of them breaks the inner loop only and the other breaks the outer loop? I can't see how this "professional mehtod" would work in such case.
@numberiforgot Жыл бұрын
This channel saves me the hassle. It’s always informative and helpful.
@frustratedalien666 Жыл бұрын
The best practice in Python is to avoid using the else block in loops. This is definitely not the canonical way of breaking out of two for loops based on a lot of python codebases I've dealt with. The else block on loops is a damn curse. It runs even if you loop over an empty sequence, which is a massive pain. I checked if this is just my opinion, but looks like the author of Effective Python has added this as Item 9 - Avoid else Blocks After for and while Loops.
@hrvojematosevic8769 Жыл бұрын
Very nice video. Always getting good valuable tips from you. Thank you.
@lucaslinhares4071 Жыл бұрын
I actually don't really like this method, because it doesn't read as easily. Declaring a variable before the loops and then checking against it reads better. And in languages that have goto, I think it's one of the few scenarios where goto are acceptable to use.
@HASVIRA_OFFICIAL Жыл бұрын
Yeah, I thought about opening my own farm, but its a long and complicated process. Im just investing in Cannafarm ltd farms and earning every day
@Anzeljaeg Жыл бұрын
Heeeyyy ty so much man
@Djellowman Жыл бұрын
I've written so much Python, but never used a for...else.
@thetruth45678 Жыл бұрын
Before watching the video, I would put a variable outside the loops, set it inside the inner loop if the condition is met, break out of the inner loop, and in the outter loop, check if the variable is set and break out of the outter loop if so. Now, let's see how it should be done.... for ... else? Okay. Now you have my attention.
@demonslayer1162 Жыл бұрын
Neat trick.
@ahmehhhd Жыл бұрын
why not make a video about algorithms time complexity?
@kayf7073 Жыл бұрын
Probably error handling would be clearer
@bhaveshverma8629 Жыл бұрын
There is no thing as break 2 ?
@AidanCanavan-v5i Жыл бұрын
Stop writing loops in python
@tonchozhelev Жыл бұрын
This kinda thing really makes me wish we still had 'goto' in modern languages. Seriously, breaking out of nested structures, and error handling, are both, easily, way better solved by using 'goto' and labels, old-school C style like. A lot more readable too, unless you overuse em, but that is what "overuse" means in the first place, so I don't really get why gotos are so hated. 'try-catch' blocks and 'for-else' statements are a lot easier to overuse imho...
@Djellowman Жыл бұрын
goto is a terrible keyword & you shouldn't use it. Lots of companies that use C have disallowed the usage of the statement.