Apr-23-2020, 04:40 PM
(This post was last modified: Apr-23-2020, 04:46 PM by ClassicalSoul.)
Please note I've made a substantive edit to my original post
Suppose you had the following code, where
Suppose you had the following code, where
x is an iterable:for i in x:
# do thing 1
# do thing 2
if <condition>:
# do thing 3
# do thing 4
continueBut after the condition is met the first time you no longer wish to do thing 4. We could try:x_iter = iter(x)
for i in x:
# do thing 1
# do thing 2
if <condition>:
# do thing 3
# do thing 4
break
for i in x:
# do thing 1
# do thing 2
if <condition>:
# do thing 3But of course we would be repeating code. My question is whether there some shortcut to what I do in the second example.
