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

  Рет қаралды 7,631

NeuralNine

NeuralNine

Күн бұрын

Пікірлер: 39
@borbelylevici
@borbelylevici Жыл бұрын
Nice trick, but maybe cleaner if you embed the nested loops in a function and just use return
@pawlox1300
@pawlox1300 Жыл бұрын
true
@piotrmazgaj
@piotrmazgaj Жыл бұрын
Nah, it's fine.
@jumper0122
@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
@k.chriscaldwell4141 Жыл бұрын
Yeah, that’s how I do it.
@DmitryRomanov
@DmitryRomanov Жыл бұрын
Or we may try/except and raise the exception. Or construct specific `with` for the purpose.
@the_huge_knight
@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
@jvsonyt Жыл бұрын
This is great. Couldn't be more perfect timing for optimizing my current project
@loc4725
@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
@vietndk5437 Жыл бұрын
Your explanation is helping my mind explore more about for loop and its application. Many thanks.
@rupen42
@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
@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
@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
@cookiebinary Жыл бұрын
PHP (and some other languages) just add the number of cycles to break: `break 2` (that is, breaks 2 cycles)
@aouerfelli
@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
@numberiforgot Жыл бұрын
This channel saves me the hassle. It’s always informative and helpful.
@frustratedalien666
@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
@hrvojematosevic8769 Жыл бұрын
Very nice video. Always getting good valuable tips from you. Thank you.
@lucaslinhares4071
@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
@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
@Anzeljaeg Жыл бұрын
Heeeyyy ty so much man
@Djellowman
@Djellowman Жыл бұрын
I've written so much Python, but never used a for...else.
@thetruth45678
@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
@demonslayer1162 Жыл бұрын
Neat trick.
@ahmehhhd
@ahmehhhd Жыл бұрын
why not make a video about algorithms time complexity?
@kayf7073
@kayf7073 Жыл бұрын
Probably error handling would be clearer
@bhaveshverma8629
@bhaveshverma8629 Жыл бұрын
There is no thing as break 2 ?
@AidanCanavan-v5i
@AidanCanavan-v5i Жыл бұрын
Stop writing loops in python
@tonchozhelev
@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
@Djellowman Жыл бұрын
goto is a terrible keyword & you shouldn't use it. Lots of companies that use C have disallowed the usage of the statement.
F-Strings Have A Lot of Format Modifiers You Don't Know
17:24
NeuralNine
Рет қаралды 11 М.
Garbage Collection in Python: Speed Up Your Code
16:41
NeuralNine
Рет қаралды 17 М.
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 7 МЛН
Car Bubble vs Lamborghini
00:33
Stokes Twins
Рет қаралды 36 МЛН
龟兔赛跑:好可爱的小乌龟#short #angel #clown
01:00
Super Beauty team
Рет қаралды 132 МЛН
Which team will win? Team Joy or Team Gumball?! 🤔
00:29
BigSchool
Рет қаралды 14 МЛН
JSON Schema Validation in Python: Bring Structure Into JSON
13:45
Python Tips and Tricks: Breaking out of Nested Loops
18:46
MathByte Academy
Рет қаралды 2,5 М.
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,8 МЛН
NumPy Full Python Course - Data Science Fundamentals
52:25
NeuralNine
Рет қаралды 89 М.
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 155 М.
What's New in Python 3.12?
14:36
NeuralNine
Рет қаралды 14 М.
Test-Driven Development in Python: Test First Code Later
15:44
NeuralNine
Рет қаралды 12 М.
Pretty Error Messages in Python
12:41
NeuralNine
Рет қаралды 7 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
бабл ти гель для душа // Eva mash
01:00
EVA mash
Рет қаралды 7 МЛН