Single line if statement with a continue

Dennis Lee Bieber wlfraed at ix.netcom.com
Sun Dec 18 11:07:18 EST 2022


On Wed, 14 Dec 2022 10:53:10 -0800 (PST), Aaron P
<transreductionist at gmail.com> declaimed the following:

	Late response here, and the concept may have been covered in
skimmed-over posts..

>I occasionally run across something like:
>
>for idx, thing in enumerate(things):
>    if idx == 103: 
>        continue
>    do_something_with(thing)
>

	For this example, I'd probably reverse the condition.

	if idx != 103:
		do_something_with(thing)

and hence completely drop the "continue" -- after all, if idx is 103, the
if statement falls through, and the end of the loop acts as an implicit
"continue"

	OTOH: if the "if/continue" is buried in four or five layers of
conditionals, it could be cleaner than trying to configure the conditionals
to have a chained exit.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/


More information about the Python-list mailing list