Python - For Loop
Last Updated: 2149Z 13AUG20
(Created: 2149Z 13AUG20)
For loops are used to iteratre over an interator.
Use only when either the functionality can't be accomplished with a {dict, list} comprehension and/or the syntax would be too unreadable.
# standard search for item in items
for item in iterator:
if item == "something":
result = do_something(item)
break # we found the target and can exit
else:
# optional
# Loop fell through without finding the target
do_something_else()
# use continue to skip expensive processing on non-interesting items
for item in iterator:
if item == "something else":
continue # Skip to next item in iterator
do_something_expensive(item)
References: