Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Foreach loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Python === {{Main|Python (programming language)}} <syntaxhighlight lang="python"> for item in iterable_collection: # Do something with item </syntaxhighlight> Python's tuple assignment, fully available in its foreach loop, also makes it trivial to iterate on (key, value) pairs in [[Associative array|dictionaries]]: <syntaxhighlight lang="python"> for key, value in some_dict.items(): # Direct iteration on a dict iterates on its keys # Do stuff </syntaxhighlight> As <code>for ... in</code> is the only kind of for loop in Python, the equivalent to the "counter" loop found in other languages is... <syntaxhighlight lang="python"> for i in range(len(seq)): # Do something to seq[i] </syntaxhighlight> ... although using the <code>enumerate</code> function is considered more "Pythonic": <syntaxhighlight lang="python"> for i, item in enumerate(seq): # Do stuff with item # Possibly assign it back to seq[i] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)