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

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

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.
@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
@vietndk5437
@vietndk5437 Жыл бұрын
Your explanation is helping my mind explore more about for loop and its application. Many thanks.
@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.
@cookiebinary
@cookiebinary Жыл бұрын
PHP (and some other languages) just add the number of cycles to break: `break 2` (that is, breaks 2 cycles)
@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.
@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.
@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.
@bhaveshverma8629
@bhaveshverma8629 Жыл бұрын
There is no thing as break 2 ?
@Anzeljaeg
@Anzeljaeg Жыл бұрын
Heeeyyy ty so much man
@ahmehhhd
@ahmehhhd Жыл бұрын
why not make a video about algorithms time complexity?
@Djellowman
@Djellowman Жыл бұрын
I've written so much Python, but never used a for...else.
@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
@demonslayer1162
@demonslayer1162 Жыл бұрын
Neat trick.
@kayf7073
@kayf7073 Жыл бұрын
Probably error handling would be clearer
@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.
@AidanCanavan-v5i
@AidanCanavan-v5i Жыл бұрын
Stop writing loops in python
F-Strings Have A Lot of Format Modifiers You Don't Know
17:24
NeuralNine
Рет қаралды 12 М.
Python Tips and Tricks: Breaking out of Nested Loops
18:46
MathByte Academy
Рет қаралды 2,6 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
NordVPN Coupon Code | Get THE BEST NordVPN Coupon Code Deal
1:27
The Techno Galaxy
Рет қаралды 690
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 166 М.
Garbage Collection in Python: Speed Up Your Code
16:41
NeuralNine
Рет қаралды 19 М.
Why You Shouldn't Nest Your Code
8:30
CodeAesthetic
Рет қаралды 2,8 МЛН
6 Tips to write BETTER For Loops in Python
9:19
Patrick Loeber
Рет қаралды 251 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 453 М.
A Deep Dive Into Iterators and Itertools in Python
21:01
ArjanCodes
Рет қаралды 62 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,8 МЛН
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 260 М.
The Fastest Way to Loop in Python - An Unfortunate Truth
8:06
mCoding
Рет қаралды 1,4 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН