| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.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="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="genexpr.html" /> |
| 13 | <link rel="prev" href="parenthesized.html" /> |
| 14 | <link rel="parent" href="atoms.html" /> |
| 15 | <link rel="next" href="genexpr.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.2.4 List displays</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="5.2.3 Parenthesized forms" |
| 25 | href="parenthesized.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="5.2 Atoms" |
| 28 | href="atoms.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="5.2.5 Generator expressions" |
| 31 | href="genexpr.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="parenthesized.html">5.2.3 Parenthesized forms</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="atoms.html">5.2 Atoms</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="genexpr.html">5.2.5 Generator expressions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION007240000000000000000"></A><A NAME="lists"></A> |
| 56 | <BR> |
| 57 | 5.2.4 List displays |
| 58 | </H2> |
| 59 | <a id='l2h-344' xml:id='l2h-344'></a><a id='l2h-345' xml:id='l2h-345'></a> |
| 60 | <P> |
| 61 | A list display is a possibly empty series of expressions enclosed in |
| 62 | square brackets: |
| 63 | |
| 64 | <P> |
| 65 | <dl><dd class="grammar"> |
| 66 | <div class="productions"> |
| 67 | <table> |
| 68 | <tr> |
| 69 | <td><a id='tok-test' xml:id='tok-test'>test</a></td> |
| 70 | <td>::=</td> |
| 71 | <td><a class='grammartoken' href="Booleans.html#tok-and_test">and_test</a> ( "or" <a class='grammartoken' href="Booleans.html#tok-and_test">and_test</a> )* |
| 72 | | <a class='grammartoken' href="lambdas.html#tok-lambda_form">lambda_form</a></td></tr> |
| 73 | <tr> |
| 74 | <td><a id='tok-testlist' xml:id='tok-testlist'>testlist</a></td> |
| 75 | <td>::=</td> |
| 76 | <td><a class='grammartoken' href="lists.html#tok-test">test</a> ( "," <a class='grammartoken' href="lists.html#tok-test">test</a> )* [ "," ]</td></tr> |
| 77 | <tr> |
| 78 | <td><a id='tok-list_display' xml:id='tok-list_display'>list_display</a></td> |
| 79 | <td>::=</td> |
| 80 | <td>"[" [<a class='grammartoken' href="lists.html#tok-listmaker">listmaker</a>] "]"</td></tr> |
| 81 | <tr> |
| 82 | <td><a id='tok-listmaker' xml:id='tok-listmaker'>listmaker</a></td> |
| 83 | <td>::=</td> |
| 84 | <td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a> ( <a class='grammartoken' href="lists.html#tok-list_for">list_for</a> |
| 85 | | ( "," <a class='grammartoken' href="Booleans.html#tok-expression">expression</a> )* [","] )</td></tr> |
| 86 | <tr> |
| 87 | <td><a id='tok-list_iter' xml:id='tok-list_iter'>list_iter</a></td> |
| 88 | <td>::=</td> |
| 89 | <td><a class='grammartoken' href="lists.html#tok-list_for">list_for</a> | <a class='grammartoken' href="lists.html#tok-list_if">list_if</a></td></tr> |
| 90 | <tr> |
| 91 | <td><a id='tok-list_for' xml:id='tok-list_for'>list_for</a></td> |
| 92 | <td>::=</td> |
| 93 | <td>"for" <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a> "in" <a class='grammartoken' href="lists.html#tok-testlist">testlist</a> |
| 94 | [<a class='grammartoken' href="lists.html#tok-list_iter">list_iter</a>]</td></tr> |
| 95 | <tr> |
| 96 | <td><a id='tok-list_if' xml:id='tok-list_if'>list_if</a></td> |
| 97 | <td>::=</td> |
| 98 | <td>"if" <a class='grammartoken' href="lists.html#tok-test">test</a> [<a class='grammartoken' href="lists.html#tok-list_iter">list_iter</a>]</td></tr> |
| 99 | </table> |
| 100 | </div> |
| 101 | <a class="grammar-footer" |
| 102 | href="grammar.txt" type="text/plain" |
| 103 | >Download entire grammar as text.</a> |
| 104 | </dd></dl> |
| 105 | |
| 106 | <P> |
| 107 | A list display yields a new list object. Its contents are specified |
| 108 | by providing either a list of expressions or a list comprehension. |
| 109 | <a id='l2h-346' xml:id='l2h-346'></a>When a comma-separated list of expressions is supplied, its elements are |
| 110 | evaluated from left to right and placed into the list object in that |
| 111 | order. When a list comprehension is supplied, it consists of a |
| 112 | single expression followed by at least one <tt class="keyword">for</tt> clause and zero or |
| 113 | more <tt class="keyword">for</tt> or <tt class="keyword">if</tt> clauses. In this |
| 114 | case, the elements of the new list are those that would be produced |
| 115 | by considering each of the <tt class="keyword">for</tt> or <tt class="keyword">if</tt> clauses a block, |
| 116 | nesting from |
| 117 | left to right, and evaluating the expression to produce a list element |
| 118 | each time the innermost block is reached<A NAME="tex2html4" |
| 119 | HREF="#foot4516"><SUP>5.1</SUP></A>. |
| 120 | <a id='l2h-347' xml:id='l2h-347'></a><a id='l2h-348' xml:id='l2h-348'></a> |
| 121 | <P> |
| 122 | <BR><HR><H4>Footnotes</H4> |
| 123 | <DL> |
| 124 | <DT><A NAME="foot4516">... reached</A><A |
| 125 | href="lists.html#tex2html4"><SUP>5.1</SUP></A></DT> |
| 126 | <DD>In Python 2.3, a |
| 127 | list comprehension "leaks" the control variables of each |
| 128 | "<tt class="samp">for</tt>" it contains into the containing scope. However, this |
| 129 | behavior is deprecated, and relying on it will not work once this |
| 130 | bug is fixed in a future release |
| 131 | |
| 132 | </DD> |
| 133 | </DL> |
| 134 | <DIV CLASS="navigation"> |
| 135 | <div class='online-navigation'> |
| 136 | <p></p><hr /> |
| 137 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 138 | <tr> |
| 139 | <td class='online-navigation'><a rel="prev" title="5.2.3 Parenthesized forms" |
| 140 | href="parenthesized.html"><img src='../icons/previous.png' |
| 141 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 142 | <td class='online-navigation'><a rel="parent" title="5.2 Atoms" |
| 143 | href="atoms.html"><img src='../icons/up.png' |
| 144 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 145 | <td class='online-navigation'><a rel="next" title="5.2.5 Generator expressions" |
| 146 | href="genexpr.html"><img src='../icons/next.png' |
| 147 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 148 | <td align="center" width="100%">Python Reference Manual</td> |
| 149 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 150 | href="contents.html"><img src='../icons/contents.png' |
| 151 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 152 | <td class='online-navigation'><img src='../icons/blank.png' |
| 153 | border='0' height='32' alt='' width='32' /></td> |
| 154 | <td class='online-navigation'><a rel="index" title="Index" |
| 155 | href="genindex.html"><img src='../icons/index.png' |
| 156 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 157 | </tr></table> |
| 158 | <div class='online-navigation'> |
| 159 | <b class="navlabel">Previous:</b> |
| 160 | <a class="sectref" rel="prev" href="parenthesized.html">5.2.3 Parenthesized forms</A> |
| 161 | <b class="navlabel">Up:</b> |
| 162 | <a class="sectref" rel="parent" href="atoms.html">5.2 Atoms</A> |
| 163 | <b class="navlabel">Next:</b> |
| 164 | <a class="sectref" rel="next" href="genexpr.html">5.2.5 Generator expressions</A> |
| 165 | </div> |
| 166 | </div> |
| 167 | <hr /> |
| 168 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 169 | </DIV> |
| 170 | <!--End of Navigation Panel--> |
| 171 | <ADDRESS> |
| 172 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 173 | </ADDRESS> |
| 174 | </BODY> |
| 175 | </HTML> |