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
Iterator
(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===<!-- This section is linked from [[Associative array]] --> Iterators in [[Python (programming language)|Python]] are a fundamental part of the language and in many cases go unseen as they are implicitly used in the <code>for</code> ([[foreach]]) statement, in [[list comprehension]]s, and in [[Python syntax and semantics#Generator expressions|generator expressions]]. All of Python's standard built-in [[Collection (abstract data type)|collection]] types support iteration, as well as many classes that are part of the standard library. The following example shows typical implicit iteration over a sequence: <syntaxhighlight lang="python"> for value in sequence: print(value) </syntaxhighlight> Python dictionaries (a form of [[associative array]]) can also be directly iterated over, when the dictionary keys are returned; or the <code>items()</code> method of a dictionary can be iterated over where it yields corresponding key,value pairs as a tuple: <syntaxhighlight lang="python"> for key in dictionary: value = dictionary[key] print(key, value) </syntaxhighlight> <syntaxhighlight lang="python"> for key, value in dictionary.items(): print(key, value) </syntaxhighlight> Iterators however can be used and defined explicitly. For any iterable sequence type or class, the built-in function <code>iter()</code> is used to create an iterator object. The iterator object can then be iterated with the <code>next()</code> function, which uses the <code>__next__()</code> method internally, which returns the next element in the container. (The previous statement applies to Python 3.x. In Python 2.x, the <code>next()</code> method is equivalent.) A <code>StopIteration</code> exception will be raised when no more elements are left. The following example shows an equivalent iteration over a sequence using explicit iterators: <syntaxhighlight lang="python"> it = iter(sequence) while True: try: value = it.next() # in Python 2.x value = next(it) # in Python 3.x except StopIteration: break print(value) </syntaxhighlight> Any user-defined class can support standard iteration (either implicit or explicit) by defining an <code>__iter__()</code> method that returns an iterator object. The iterator object then needs to define a <code>__next__()</code> method that returns the next element. Python's [[#Generators|generators]] implement this iteration [[Protocol (object-oriented programming)|protocol]].
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)