Python Tips and Tricks: Breaking out of Nested Loops

preview_player
Показать описание
In this video we take a look at some techniques to break out of multiple nested loops since Python does not provide a built-in language mechanism to do so. We also identify what I think is the best way to do this.

#mathbyteacademy #python

Code for this Video
================

My Python Courses
=================

- Python 3 Fundamentals (introduction to Python)

- Python 3 Deep Dive (Part 1 - Functional)

- Python 3 Deep Dive (Part 2 - Iteration, Generators)

- Python 3 Deep Dive (Part 3 - Hash Maps)

- Python 3 Deep Dive (Part 4 - OOP)
Рекомендации по теме
Комментарии
Автор

Wow i love that you gave options, thanks so much for this! I ran into a nested loop while building a card game (it loops through 2 lists and returns one card if it matches).

Thanks!!

kingalonsoiv
Автор

When I started watching the video I did think about both the last 2 possibilities. And I believe they happen to be the best, for specific use cases:
- pure nested loops the function method is the best, since you can simply wrap those in a single method and you're done
- rasising an exception I believe works the best if your nested loops are not explicit, such as in a larger codebase where you have a function that iterates over some iterable and then for each element calls another method (which may as well iterate over something else and make further calls). Returning a value would only return for the "last method", while raising an exception will only stop until it gets caught in the try/except block (and you may even re-raise the exception if the inner methods with loops also handle exceptions)

vitaum
Автор

Great content as always, thank you Maestro

AmilcarHernandezAmil
Автор

Thank you very much!

This is _very useful_ for the kind of programming I am doing, which is making midi animations with Blender. In particular, I needed to find the corresponding note-off that goes with a note-on.

In other languages, like Delphi and C#, the only way I could come up with is a GOTO command with a label. But that is not possible in Python because Python doesn't have a GOTO command and doesn't support labels! So I was _really stuck here!_

It is also nice, Fred, because I am following your Deep Dive course of Udemy, part 2, and I happen to be in the section where you deal with iterators extensively. This is a nice addition.

Your Python courses on Udemy are awesome! The only thing missing in them, is a treatment of bytestrings, which is something I really need for midi file programming. But apart from that, those courses are really good!

konradswart
Автор

Fred: This is a clever way of breaking out from nested loops. Thank you for sharing.

abpcx
Автор

As usual, a world class video. Thanks a lot!

jflc
Автор

Awesome content. Thanks!
Do you have any plans for unit testing tutorial ?

dev.aaajit
Автор

Great Video Fred,
Do you plan to make similar video on "using Return in a recursive function"?

__ab
Автор

For-else is quite weird construct, but sometimes it might be handy.

Btw, any plans on teaching Rust? That would be super helpful. You are great teacher.

amidfallen
Автор

This whole operation is only a short itertools operation:

xs =
neg_xs = filter(lambda x: x[1] < 0, enumerate(xs))
try:
first_neg_x = next(neg_xs)
except StopIteration:
first_neg_x = None


Personally I'd use my own compose and take_nth functions and write it as

only_negatives = functools.partial(filter, lambda x: x[1] < 0)
first = functools.partial(take_nth, 0)

take_first_negative = compose(
flatten,
enumerate,
only_negatives,
first
)

first_neg_x = take_first_negative(xs)

julians.
Автор

when using the try-except block, wouldnt it be better to wrap the entire nested loop like this:

try:
for _ in _:
for _ in _:
...
if condition:
raise StopIteration
except StopIteration:
pass

this way, we can exit any number of nested loops and it is more readible than the example in the video as the nested loops are not interupted by the try statement.

martin-__-
Автор

i like the way u explain things, unfortunately this is the only video in ur channel that i can understand a little bit of it lol I'm still a beginner

lebleb