Stuck in Nested Loops? Here's How You Leave Them!

preview_player
Показать описание
Today we learn how to properly break out of nested loops in Python.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚

💼 Services 💼

🌐 Social Media & Contact 🌐
Рекомендации по теме
Комментарии
Автор

Nice trick, but maybe cleaner if you embed the nested loops in a function and just use return

borbelylevici
Автор

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.

the_huge_knight
Автор

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.

loc
Автор

This is great. Couldn't be more perfect timing for optimizing my current project

jvsonyt
Автор

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.

rupen
Автор

PHP (and some other languages) just add the number of cycles to break: `break 2` (that is, breaks 2 cycles)

cookiebinary
Автор

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.

aouerfelli
Автор

Your explanation is helping my mind explore more about for loop and its application. Many thanks.

vietndk
Автор

Very nice video. Always getting good valuable tips from you. Thank you.

hrvojematosevic
Автор

This channel saves me the hassle. It’s always informative and helpful.

numberiforgot
Автор

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.

lucaslinhares
Автор

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.

frustratedalien
Автор

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.

thetruth
Автор

I've written so much Python, but never used a for...else.

Djellowman
Автор

why not make a video about algorithms time complexity?

ahmehhhd
Автор

Probably error handling would be clearer

kayf
Автор

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

HASVIRA_OFFICIAL
Автор

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...

tonchozhelev
visit shbcf.ru