<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7.3 The for statement</TITLE>
<META NAME="description" CONTENT="7.3 The for statement">
<META NAME="keywords" CONTENT="ref">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ref.css">
<LINK REL="next" HREF="try.html">
<LINK REL="previous" HREF="while.html">
<LINK REL="up" HREF="compound.html">
<LINK REL="next" HREF="try.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="try.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="compound.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="while.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="try.html">7.4 The try statement</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="compound.html">7. Compound statements</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="while.html">7.2 The while statement</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009300000000000000000"></A>
<BR>
7.3 The <tt class=keyword>for</tt> statement
</H1>
<a name="l2h-683"></a>
<a name="l2h-684"></a>

<P>
The <tt class=keyword>for</tt> statement is used to iterate over the elements of a
sequence (string, tuple or list):
<a name="l2h-685"></a>

<P>
<dl><dd><pre class=verbatim>
for_stmt:       "for" target_list "in" expression_list ":" suite
               ["else" ":" suite]
</pre></dl>

<P>
The expression list is evaluated once; it should yield a sequence.  The
suite is then executed once for each item in the sequence, in the
order of ascending indices.  Each item in turn is assigned to the
target list using the standard rules for assignments, and then the
suite is executed.  When the items are exhausted (which is immediately
when the sequence is empty), the suite in the <tt class=keyword>else</tt> clause, if
present, is executed, and the loop terminates.
<a name="l2h-686"></a>
<a name="l2h-687"></a>
<a name="l2h-688"></a>

<P>
A <tt class=keyword>break</tt> statement executed in the first suite terminates the
loop without executing the <tt class=keyword>else</tt> clause's suite.  A
<tt class=keyword>continue</tt> statement executed in the first suite skips the rest
of the suite and continues with the next item, or with the <tt class=keyword>else</tt>
clause if there was no next item.
<a name="l2h-689"></a>
<a name="l2h-690"></a>

<P>
The suite may assign to the variable(s) in the target list; this does
not affect the next item assigned to it.

<P>
The target list is not deleted when the loop is finished, but if the
sequence is empty, it will not have been assigned to at all by the
loop.  Hint: the built-in function <tt class=function>range()</tt> returns a
sequence of integers suitable to emulate the effect of Pascal's
<tt>for i := a to b do</tt>;
e.g., <tt>range(3)</tt> returns the list <tt>[0, 1, 2]</tt>.
<a name="l2h-691"></a>
<a name="l2h-692"></a>

<P>
<b>Warning:</b> There is a subtlety when the sequence is being modified
by the loop (this can only occur for mutable sequences, i.e. lists).
An internal counter is used to keep track of which item is used next,
and this is incremented on each iteration.  When this counter has
reached the length of the sequence the loop terminates.  This means that
if the suite deletes the current (or a previous) item from the
sequence, the next item will be skipped (since it gets the index of
the current item which has already been treated).  Likewise, if the
suite inserts an item in the sequence before the current item, the
current item will be treated again the next time through the loop.
This can lead to nasty bugs that can be avoided by making a temporary
copy using a slice of the whole sequence, e.g.,
<a name="l2h-693"></a>
<a name="l2h-694"></a>

<P>
<dl><dd><pre class=verbatim>
for x in a[:]:
    if x &lt; 0: a.remove(x)
</pre></dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="try.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="compound.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="while.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="try.html">7.4 The try statement</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="compound.html">7. Compound statements</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="while.html">7.2 The while statement</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
