Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Iterator
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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]].
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Iterator
(section)
Add topic