Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="whatsnew24.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="whatsnew24.html" title='What's New in Python 2.4' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='last' href='about.html' title='About this document...' /> | |
10 | <link rel='help' href='about.html' title='About this document...' /> | |
11 | <link rel="next" href="node8.html" /> | |
12 | <link rel="prev" href="node6.html" /> | |
13 | <link rel="parent" href="whatsnew24.html" /> | |
14 | <link rel="next" href="node8.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>6 PEP 322: Reverse Iteration</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="5 PEP 318: Decorators" | |
24 | href="node6.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
27 | href="whatsnew24.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="7 PEP 324: New" | |
30 | href="node8.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">What's New in Python 2.4</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><img src='../icons/blank.png' | |
39 | border='0' height='32' alt='' width='32' /></td> | |
40 | </tr></table> | |
41 | <div class='online-navigation'> | |
42 | <b class="navlabel">Previous:</b> | |
43 | <a class="sectref" rel="prev" href="node6.html">5 PEP 318: Decorators</A> | |
44 | <b class="navlabel">Up:</b> | |
45 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
46 | <b class="navlabel">Next:</b> | |
47 | <a class="sectref" rel="next" href="node8.html">7 PEP 324: New</A> | |
48 | </div> | |
49 | <hr /></div> | |
50 | </DIV> | |
51 | <!--End of Navigation Panel--> | |
52 | ||
53 | <H1><A NAME="SECTION000700000000000000000"> | |
54 | 6 PEP 322: Reverse Iteration</A> | |
55 | </H1> | |
56 | ||
57 | <P> | |
58 | A new built-in function, <tt class="function">reversed(<var>seq</var>)</tt>, takes a sequence | |
59 | and returns an iterator that loops over the elements of the sequence | |
60 | in reverse order. | |
61 | ||
62 | <P> | |
63 | <div class="verbatim"><pre> | |
64 | >>> for i in reversed(xrange(1,4)): | |
65 | ... print i | |
66 | ... | |
67 | 3 | |
68 | 2 | |
69 | 1 | |
70 | </pre></div> | |
71 | ||
72 | <P> | |
73 | Compared to extended slicing, such as <code>range(1,4)[::-1]</code>, | |
74 | <tt class="function">reversed()</tt> is easier to read, runs faster, and uses | |
75 | substantially less memory. | |
76 | ||
77 | <P> | |
78 | Note that <tt class="function">reversed()</tt> only accepts sequences, not arbitrary | |
79 | iterators. If you want to reverse an iterator, first convert it to | |
80 | a list with <tt class="function">list()</tt>. | |
81 | ||
82 | <P> | |
83 | <div class="verbatim"><pre> | |
84 | >>> input = open('/etc/passwd', 'r') | |
85 | >>> for line in reversed(list(input)): | |
86 | ... print line | |
87 | ... | |
88 | root:*:0:0:System Administrator:/var/root:/bin/tcsh | |
89 | ... | |
90 | </pre></div> | |
91 | ||
92 | <P> | |
93 | <div class="seealso"> | |
94 | <p class="heading">See Also:</p> | |
95 | ||
96 | <dl compact="compact" class="seerfc"> | |
97 | <dt><a href="http://www.python.org/peps/pep-0322.html" | |
98 | title="Reverse Iteration" | |
99 | >PEP 322, <em>Reverse Iteration</em></a> | |
100 | <dd>Written and implemented by Raymond Hettinger. | |
101 | </dl> | |
102 | ||
103 | <P> | |
104 | </div> | |
105 | ||
106 | <P> | |
107 | ||
108 | <DIV CLASS="navigation"> | |
109 | <div class='online-navigation'> | |
110 | <p></p><hr /> | |
111 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
112 | <tr> | |
113 | <td class='online-navigation'><a rel="prev" title="5 PEP 318: Decorators" | |
114 | href="node6.html"><img src='../icons/previous.png' | |
115 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
116 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
117 | href="whatsnew24.html"><img src='../icons/up.png' | |
118 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
119 | <td class='online-navigation'><a rel="next" title="7 PEP 324: New" | |
120 | href="node8.html"><img src='../icons/next.png' | |
121 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
122 | <td align="center" width="100%">What's New in Python 2.4</td> | |
123 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
124 | href="contents.html"><img src='../icons/contents.png' | |
125 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
126 | <td class='online-navigation'><img src='../icons/blank.png' | |
127 | border='0' height='32' alt='' width='32' /></td> | |
128 | <td class='online-navigation'><img src='../icons/blank.png' | |
129 | border='0' height='32' alt='' width='32' /></td> | |
130 | </tr></table> | |
131 | <div class='online-navigation'> | |
132 | <b class="navlabel">Previous:</b> | |
133 | <a class="sectref" rel="prev" href="node6.html">5 PEP 318: Decorators</A> | |
134 | <b class="navlabel">Up:</b> | |
135 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
136 | <b class="navlabel">Next:</b> | |
137 | <a class="sectref" rel="next" href="node8.html">7 PEP 324: New</A> | |
138 | </div> | |
139 | </div> | |
140 | <hr /> | |
141 | <span class="release-info">Release 1.01.</span> | |
142 | </DIV> | |
143 | <!--End of Navigation Panel--> | |
144 | <ADDRESS> | |
145 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
146 | </ADDRESS> | |
147 | </BODY> | |
148 | </HTML> |