datastructures.html 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="5. Data Structures" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/tutorial/datastructures.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>5. Data Structures &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="6. Modules" href="modules.html" />
  34. <link rel="prev" title="4. More Control Flow Tools" href="controlflow.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/tutorial/datastructures.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">5. Data Structures</a><ul>
  86. <li><a class="reference internal" href="#more-on-lists">5.1. More on Lists</a><ul>
  87. <li><a class="reference internal" href="#using-lists-as-stacks">5.1.1. Using Lists as Stacks</a></li>
  88. <li><a class="reference internal" href="#using-lists-as-queues">5.1.2. Using Lists as Queues</a></li>
  89. <li><a class="reference internal" href="#list-comprehensions">5.1.3. List Comprehensions</a></li>
  90. <li><a class="reference internal" href="#nested-list-comprehensions">5.1.4. Nested List Comprehensions</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#the-del-statement">5.2. The <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> statement</a></li>
  94. <li><a class="reference internal" href="#tuples-and-sequences">5.3. Tuples and Sequences</a></li>
  95. <li><a class="reference internal" href="#sets">5.4. Sets</a></li>
  96. <li><a class="reference internal" href="#dictionaries">5.5. Dictionaries</a></li>
  97. <li><a class="reference internal" href="#looping-techniques">5.6. Looping Techniques</a></li>
  98. <li><a class="reference internal" href="#more-on-conditions">5.7. More on Conditions</a></li>
  99. <li><a class="reference internal" href="#comparing-sequences-and-other-types">5.8. Comparing Sequences and Other Types</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. </div>
  104. <div>
  105. <h4>Previous topic</h4>
  106. <p class="topless"><a href="controlflow.html"
  107. title="previous chapter"><span class="section-number">4. </span>More Control Flow Tools</a></p>
  108. </div>
  109. <div>
  110. <h4>Next topic</h4>
  111. <p class="topless"><a href="modules.html"
  112. title="next chapter"><span class="section-number">6. </span>Modules</a></p>
  113. </div>
  114. <div role="note" aria-label="source link">
  115. <h3>This Page</h3>
  116. <ul class="this-page-menu">
  117. <li><a href="../bugs.html">Report a Bug</a></li>
  118. <li>
  119. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/datastructures.rst"
  120. rel="nofollow">Show Source
  121. </a>
  122. </li>
  123. </ul>
  124. </div>
  125. </nav>
  126. </div>
  127. </div>
  128. <div class="related" role="navigation" aria-label="related navigation">
  129. <h3>Navigation</h3>
  130. <ul>
  131. <li class="right" style="margin-right: 10px">
  132. <a href="../genindex.html" title="General Index"
  133. accesskey="I">index</a></li>
  134. <li class="right" >
  135. <a href="../py-modindex.html" title="Python Module Index"
  136. >modules</a> |</li>
  137. <li class="right" >
  138. <a href="modules.html" title="6. Modules"
  139. accesskey="N">next</a> |</li>
  140. <li class="right" >
  141. <a href="controlflow.html" title="4. More Control Flow Tools"
  142. accesskey="P">previous</a> |</li>
  143. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  144. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  145. <li class="switchers">
  146. <div class="language_switcher_placeholder"></div>
  147. <div class="version_switcher_placeholder"></div>
  148. </li>
  149. <li>
  150. </li>
  151. <li id="cpython-language-and-version">
  152. <a href="../index.html">3.12.0 Documentation</a> &#187;
  153. </li>
  154. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Tutorial</a> &#187;</li>
  155. <li class="nav-item nav-item-this"><a href=""><span class="section-number">5. </span>Data Structures</a></li>
  156. <li class="right">
  157. <div class="inline-search" role="search">
  158. <form class="inline-search" action="../search.html" method="get">
  159. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  160. <input type="submit" value="Go" />
  161. </form>
  162. </div>
  163. |
  164. </li>
  165. <li class="right">
  166. <label class="theme-selector-label">
  167. Theme
  168. <select class="theme-selector" oninput="activateTheme(this.value)">
  169. <option value="auto" selected>Auto</option>
  170. <option value="light">Light</option>
  171. <option value="dark">Dark</option>
  172. </select>
  173. </label> |</li>
  174. </ul>
  175. </div>
  176. <div class="document">
  177. <div class="documentwrapper">
  178. <div class="bodywrapper">
  179. <div class="body" role="main">
  180. <section id="data-structures">
  181. <span id="tut-structures"></span><h1><span class="section-number">5. </span>Data Structures<a class="headerlink" href="#data-structures" title="Permalink to this headline">¶</a></h1>
  182. <p>This chapter describes some things you’ve learned about already in more detail,
  183. and adds some new things as well.</p>
  184. <section id="more-on-lists">
  185. <span id="tut-morelists"></span><h2><span class="section-number">5.1. </span>More on Lists<a class="headerlink" href="#more-on-lists" title="Permalink to this headline">¶</a></h2>
  186. <p>The list data type has some more methods. Here are all of the methods of list
  187. objects:</p>
  188. <dl class="py method">
  189. <dt class="sig sig-object py">
  190. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">append</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span></dt>
  191. <dd><p>Add an item to the end of the list. Equivalent to <code class="docutils literal notranslate"><span class="pre">a[len(a):]</span> <span class="pre">=</span> <span class="pre">[x]</span></code>.</p>
  192. </dd></dl>
  193. <dl class="py method">
  194. <dt class="sig sig-object py">
  195. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">extend</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">iterable</span></span></em><span class="sig-paren">)</span></dt>
  196. <dd><p>Extend the list by appending all the items from the iterable. Equivalent to
  197. <code class="docutils literal notranslate"><span class="pre">a[len(a):]</span> <span class="pre">=</span> <span class="pre">iterable</span></code>.</p>
  198. </dd></dl>
  199. <dl class="py method">
  200. <dt class="sig sig-object py">
  201. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">insert</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span></dt>
  202. <dd><p>Insert an item at a given position. The first argument is the index of the
  203. element before which to insert, so <code class="docutils literal notranslate"><span class="pre">a.insert(0,</span> <span class="pre">x)</span></code> inserts at the front of
  204. the list, and <code class="docutils literal notranslate"><span class="pre">a.insert(len(a),</span> <span class="pre">x)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">a.append(x)</span></code>.</p>
  205. </dd></dl>
  206. <dl class="py method">
  207. <dt class="sig sig-object py">
  208. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span></dt>
  209. <dd><p>Remove the first item from the list whose value is equal to <em>x</em>. It raises a
  210. <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if there is no such item.</p>
  211. </dd></dl>
  212. <dl class="py method">
  213. <dt class="sig sig-object py">
  214. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">pop</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em><span class="optional">]</span><span class="sig-paren">)</span></dt>
  215. <dd><p>Remove the item at the given position in the list, and return it. If no index
  216. is specified, <code class="docutils literal notranslate"><span class="pre">a.pop()</span></code> removes and returns the last item in the list. (The
  217. square brackets around the <em>i</em> in the method signature denote that the parameter
  218. is optional, not that you should type square brackets at that position. You
  219. will see this notation frequently in the Python Library Reference.)</p>
  220. </dd></dl>
  221. <dl class="py method">
  222. <dt class="sig sig-object py">
  223. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
  224. <dd><p>Remove all items from the list. Equivalent to <code class="docutils literal notranslate"><span class="pre">del</span> <span class="pre">a[:]</span></code>.</p>
  225. </dd></dl>
  226. <dl class="py method">
  227. <dt class="sig sig-object py">
  228. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">index</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">start</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">end</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
  229. <dd><p>Return zero-based index in the list of the first item whose value is equal to <em>x</em>.
  230. Raises a <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if there is no such item.</p>
  231. <p>The optional arguments <em>start</em> and <em>end</em> are interpreted as in the slice
  232. notation and are used to limit the search to a particular subsequence of
  233. the list. The returned index is computed relative to the beginning of the full
  234. sequence rather than the <em>start</em> argument.</p>
  235. </dd></dl>
  236. <dl class="py method">
  237. <dt class="sig sig-object py">
  238. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span></dt>
  239. <dd><p>Return the number of times <em>x</em> appears in the list.</p>
  240. </dd></dl>
  241. <dl class="py method">
  242. <dt class="sig sig-object py">
  243. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">sort</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reverse</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span></dt>
  244. <dd><p>Sort the items of the list in place (the arguments can be used for sort
  245. customization, see <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a> for their explanation).</p>
  246. </dd></dl>
  247. <dl class="py method">
  248. <dt class="sig sig-object py">
  249. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">reverse</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
  250. <dd><p>Reverse the elements of the list in place.</p>
  251. </dd></dl>
  252. <dl class="py method">
  253. <dt class="sig sig-object py">
  254. <span class="sig-prename descclassname"><span class="pre">list.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
  255. <dd><p>Return a shallow copy of the list. Equivalent to <code class="docutils literal notranslate"><span class="pre">a[:]</span></code>.</p>
  256. </dd></dl>
  257. <p>An example that uses most of the list methods:</p>
  258. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;pear&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">,</span> <span class="s1">&#39;kiwi&#39;</span><span class="p">,</span> <span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">]</span>
  259. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;apple&#39;</span><span class="p">)</span>
  260. <span class="go">2</span>
  261. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;tangerine&#39;</span><span class="p">)</span>
  262. <span class="go">0</span>
  263. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;banana&#39;</span><span class="p">)</span>
  264. <span class="go">3</span>
  265. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;banana&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="c1"># Find next banana starting at position 4</span>
  266. <span class="go">6</span>
  267. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
  268. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span>
  269. <span class="go">[&#39;banana&#39;, &#39;apple&#39;, &#39;kiwi&#39;, &#39;banana&#39;, &#39;pear&#39;, &#39;apple&#39;, &#39;orange&#39;]</span>
  270. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;grape&#39;</span><span class="p">)</span>
  271. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span>
  272. <span class="go">[&#39;banana&#39;, &#39;apple&#39;, &#39;kiwi&#39;, &#39;banana&#39;, &#39;pear&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;grape&#39;]</span>
  273. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>
  274. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span>
  275. <span class="go">[&#39;apple&#39;, &#39;apple&#39;, &#39;banana&#39;, &#39;banana&#39;, &#39;grape&#39;, &#39;kiwi&#39;, &#39;orange&#39;, &#39;pear&#39;]</span>
  276. <span class="gp">&gt;&gt;&gt; </span><span class="n">fruits</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
  277. <span class="go">&#39;pear&#39;</span>
  278. </pre></div>
  279. </div>
  280. <p>You might have noticed that methods like <code class="docutils literal notranslate"><span class="pre">insert</span></code>, <code class="docutils literal notranslate"><span class="pre">remove</span></code> or <code class="docutils literal notranslate"><span class="pre">sort</span></code> that
  281. only modify the list have no return value printed – they return the default
  282. <code class="docutils literal notranslate"><span class="pre">None</span></code>. <a class="footnote-reference brackets" href="#id2" id="id1">1</a> This is a design principle for all mutable data structures in
  283. Python.</p>
  284. <p>Another thing you might notice is that not all data can be sorted or
  285. compared. For instance, <code class="docutils literal notranslate"><span class="pre">[None,</span> <span class="pre">'hello',</span> <span class="pre">10]</span></code> doesn’t sort because
  286. integers can’t be compared to strings and <em>None</em> can’t be compared to
  287. other types. Also, there are some types that don’t have a defined
  288. ordering relation. For example, <code class="docutils literal notranslate"><span class="pre">3+4j</span> <span class="pre">&lt;</span> <span class="pre">5+7j</span></code> isn’t a valid
  289. comparison.</p>
  290. <section id="using-lists-as-stacks">
  291. <span id="tut-lists-as-stacks"></span><h3><span class="section-number">5.1.1. </span>Using Lists as Stacks<a class="headerlink" href="#using-lists-as-stacks" title="Permalink to this headline">¶</a></h3>
  292. <p>The list methods make it very easy to use a list as a stack, where the last
  293. element added is the first element retrieved (“last-in, first-out”). To add an
  294. item to the top of the stack, use <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code>. To retrieve an item from the
  295. top of the stack, use <code class="xref py py-meth docutils literal notranslate"><span class="pre">pop()</span></code> without an explicit index. For example:</p>
  296. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span> <span class="o">=</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">]</span>
  297. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
  298. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
  299. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span>
  300. <span class="go">[3, 4, 5, 6, 7]</span>
  301. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
  302. <span class="go">7</span>
  303. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span>
  304. <span class="go">[3, 4, 5, 6]</span>
  305. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
  306. <span class="go">6</span>
  307. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
  308. <span class="go">5</span>
  309. <span class="gp">&gt;&gt;&gt; </span><span class="n">stack</span>
  310. <span class="go">[3, 4]</span>
  311. </pre></div>
  312. </div>
  313. </section>
  314. <section id="using-lists-as-queues">
  315. <span id="tut-lists-as-queues"></span><h3><span class="section-number">5.1.2. </span>Using Lists as Queues<a class="headerlink" href="#using-lists-as-queues" title="Permalink to this headline">¶</a></h3>
  316. <p>It is also possible to use a list as a queue, where the first element added is
  317. the first element retrieved (“first-in, first-out”); however, lists are not
  318. efficient for this purpose. While appends and pops from the end of list are
  319. fast, doing inserts or pops from the beginning of a list is slow (because all
  320. of the other elements have to be shifted by one).</p>
  321. <p>To implement a queue, use <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.deque</span></code></a> which was designed to
  322. have fast appends and pops from both ends. For example:</p>
  323. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
  324. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="o">=</span> <span class="n">deque</span><span class="p">([</span><span class="s2">&quot;Eric&quot;</span><span class="p">,</span> <span class="s2">&quot;John&quot;</span><span class="p">,</span> <span class="s2">&quot;Michael&quot;</span><span class="p">])</span>
  325. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Terry&quot;</span><span class="p">)</span> <span class="c1"># Terry arrives</span>
  326. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Graham&quot;</span><span class="p">)</span> <span class="c1"># Graham arrives</span>
  327. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c1"># The first to arrive now leaves</span>
  328. <span class="go">&#39;Eric&#39;</span>
  329. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c1"># The second to arrive now leaves</span>
  330. <span class="go">&#39;John&#39;</span>
  331. <span class="gp">&gt;&gt;&gt; </span><span class="n">queue</span> <span class="c1"># Remaining queue in order of arrival</span>
  332. <span class="go">deque([&#39;Michael&#39;, &#39;Terry&#39;, &#39;Graham&#39;])</span>
  333. </pre></div>
  334. </div>
  335. </section>
  336. <section id="list-comprehensions">
  337. <span id="tut-listcomps"></span><h3><span class="section-number">5.1.3. </span>List Comprehensions<a class="headerlink" href="#list-comprehensions" title="Permalink to this headline">¶</a></h3>
  338. <p>List comprehensions provide a concise way to create lists.
  339. Common applications are to make new lists where each element is the result of
  340. some operations applied to each member of another sequence or iterable, or to
  341. create a subsequence of those elements that satisfy a certain condition.</p>
  342. <p>For example, assume we want to create a list of squares, like:</p>
  343. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">squares</span> <span class="o">=</span> <span class="p">[]</span>
  344. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
  345. <span class="gp">... </span> <span class="n">squares</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
  346. <span class="gp">...</span>
  347. <span class="gp">&gt;&gt;&gt; </span><span class="n">squares</span>
  348. <span class="go">[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span>
  349. </pre></div>
  350. </div>
  351. <p>Note that this creates (or overwrites) a variable named <code class="docutils literal notranslate"><span class="pre">x</span></code> that still exists
  352. after the loop completes. We can calculate the list of squares without any
  353. side effects using:</p>
  354. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">squares</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)))</span>
  355. </pre></div>
  356. </div>
  357. <p>or, equivalently:</p>
  358. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">squares</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
  359. </pre></div>
  360. </div>
  361. <p>which is more concise and readable.</p>
  362. <p>A list comprehension consists of brackets containing an expression followed
  363. by a <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code> clause, then zero or more <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code> or <code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code>
  364. clauses. The result will be a new list resulting from evaluating the expression
  365. in the context of the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code> and <code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code> clauses which follow it.
  366. For example, this listcomp combines the elements of two lists if they are not
  367. equal:</p>
  368. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span> <span class="k">if</span> <span class="n">x</span> <span class="o">!=</span> <span class="n">y</span><span class="p">]</span>
  369. <span class="go">[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span>
  370. </pre></div>
  371. </div>
  372. <p>and it’s equivalent to:</p>
  373. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">combs</span> <span class="o">=</span> <span class="p">[]</span>
  374. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">]:</span>
  375. <span class="gp">... </span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">]:</span>
  376. <span class="gp">... </span> <span class="k">if</span> <span class="n">x</span> <span class="o">!=</span> <span class="n">y</span><span class="p">:</span>
  377. <span class="gp">... </span> <span class="n">combs</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
  378. <span class="gp">...</span>
  379. <span class="gp">&gt;&gt;&gt; </span><span class="n">combs</span>
  380. <span class="go">[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span>
  381. </pre></div>
  382. </div>
  383. <p>Note how the order of the <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> and <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> statements is the
  384. same in both these snippets.</p>
  385. <p>If the expression is a tuple (e.g. the <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span></code> in the previous example),
  386. it must be parenthesized.</p>
  387. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">vec</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
  388. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># create a new list with the values doubled</span>
  389. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">x</span><span class="o">*</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">vec</span><span class="p">]</span>
  390. <span class="go">[-8, -4, 0, 4, 8]</span>
  391. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># filter the list to exclude negative numbers</span>
  392. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">vec</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">]</span>
  393. <span class="go">[0, 2, 4]</span>
  394. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># apply a function to all the elements</span>
  395. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="nb">abs</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">vec</span><span class="p">]</span>
  396. <span class="go">[4, 2, 0, 2, 4]</span>
  397. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># call a method on each element</span>
  398. <span class="gp">&gt;&gt;&gt; </span><span class="n">freshfruit</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39; banana&#39;</span><span class="p">,</span> <span class="s1">&#39; loganberry &#39;</span><span class="p">,</span> <span class="s1">&#39;passion fruit &#39;</span><span class="p">]</span>
  399. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">weapon</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">weapon</span> <span class="ow">in</span> <span class="n">freshfruit</span><span class="p">]</span>
  400. <span class="go">[&#39;banana&#39;, &#39;loganberry&#39;, &#39;passion fruit&#39;]</span>
  401. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># create a list of 2-tuples like (number, square)</span>
  402. <span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)]</span>
  403. <span class="go">[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]</span>
  404. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># the tuple must be parenthesized, otherwise an error is raised</span>
  405. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)]</span>
  406. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>
  407. <span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)]</span>
  408. <span class="w"> </span><span class="pm">^^^^^^^</span>
  409. <span class="gr">SyntaxError</span>: <span class="n">did you forget parentheses around the comprehension target?</span>
  410. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># flatten a list using a listcomp with two &#39;for&#39;</span>
  411. <span class="gp">&gt;&gt;&gt; </span><span class="n">vec</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span><span class="p">]]</span>
  412. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">num</span> <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">vec</span> <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">elem</span><span class="p">]</span>
  413. <span class="go">[1, 2, 3, 4, 5, 6, 7, 8, 9]</span>
  414. </pre></div>
  415. </div>
  416. <p>List comprehensions can contain complex expressions and nested functions:</p>
  417. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">pi</span>
  418. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">pi</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">)]</span>
  419. <span class="go">[&#39;3.1&#39;, &#39;3.14&#39;, &#39;3.142&#39;, &#39;3.1416&#39;, &#39;3.14159&#39;]</span>
  420. </pre></div>
  421. </div>
  422. </section>
  423. <section id="nested-list-comprehensions">
  424. <h3><span class="section-number">5.1.4. </span>Nested List Comprehensions<a class="headerlink" href="#nested-list-comprehensions" title="Permalink to this headline">¶</a></h3>
  425. <p>The initial expression in a list comprehension can be any arbitrary expression,
  426. including another list comprehension.</p>
  427. <p>Consider the following example of a 3x4 matrix implemented as a list of
  428. 3 lists of length 4:</p>
  429. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">matrix</span> <span class="o">=</span> <span class="p">[</span>
  430. <span class="gp">... </span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">],</span>
  431. <span class="gp">... </span> <span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">],</span>
  432. <span class="gp">... </span> <span class="p">[</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">12</span><span class="p">],</span>
  433. <span class="gp">... </span><span class="p">]</span>
  434. </pre></div>
  435. </div>
  436. <p>The following list comprehension will transpose rows and columns:</p>
  437. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[[</span><span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">matrix</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)]</span>
  438. <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span>
  439. </pre></div>
  440. </div>
  441. <p>As we saw in the previous section, the inner list comprehension is evaluated in
  442. the context of the <a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> that follows it, so this example is
  443. equivalent to:</p>
  444. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">transposed</span> <span class="o">=</span> <span class="p">[]</span>
  445. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
  446. <span class="gp">... </span> <span class="n">transposed</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">matrix</span><span class="p">])</span>
  447. <span class="gp">...</span>
  448. <span class="gp">&gt;&gt;&gt; </span><span class="n">transposed</span>
  449. <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span>
  450. </pre></div>
  451. </div>
  452. <p>which, in turn, is the same as:</p>
  453. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">transposed</span> <span class="o">=</span> <span class="p">[]</span>
  454. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
  455. <span class="gp">... </span> <span class="c1"># the following 3 lines implement the nested listcomp</span>
  456. <span class="gp">... </span> <span class="n">transposed_row</span> <span class="o">=</span> <span class="p">[]</span>
  457. <span class="gp">... </span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">matrix</span><span class="p">:</span>
  458. <span class="gp">... </span> <span class="n">transposed_row</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
  459. <span class="gp">... </span> <span class="n">transposed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">transposed_row</span><span class="p">)</span>
  460. <span class="gp">...</span>
  461. <span class="gp">&gt;&gt;&gt; </span><span class="n">transposed</span>
  462. <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span>
  463. </pre></div>
  464. </div>
  465. <p>In the real world, you should prefer built-in functions to complex flow statements.
  466. The <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> function would do a great job for this use case:</p>
  467. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">matrix</span><span class="p">))</span>
  468. <span class="go">[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]</span>
  469. </pre></div>
  470. </div>
  471. <p>See <a class="reference internal" href="controlflow.html#tut-unpacking-arguments"><span class="std std-ref">Unpacking Argument Lists</span></a> for details on the asterisk in this line.</p>
  472. </section>
  473. </section>
  474. <section id="the-del-statement">
  475. <span id="tut-del"></span><h2><span class="section-number">5.2. </span>The <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> statement<a class="headerlink" href="#the-del-statement" title="Permalink to this headline">¶</a></h2>
  476. <p>There is a way to remove an item from a list given its index instead of its
  477. value: the <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> statement. This differs from the <code class="xref py py-meth docutils literal notranslate"><span class="pre">pop()</span></code> method
  478. which returns a value. The <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> statement can also be used to remove
  479. slices from a list or clear the entire list (which we did earlier by assignment
  480. of an empty list to the slice). For example:</p>
  481. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mf">66.25</span><span class="p">,</span> <span class="mi">333</span><span class="p">,</span> <span class="mi">333</span><span class="p">,</span> <span class="mf">1234.5</span><span class="p">]</span>
  482. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  483. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  484. <span class="go">[1, 66.25, 333, 333, 1234.5]</span>
  485. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
  486. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  487. <span class="go">[1, 66.25, 1234.5]</span>
  488. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span><span class="p">[:]</span>
  489. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  490. <span class="go">[]</span>
  491. </pre></div>
  492. </div>
  493. <p><a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> can also be used to delete entire variables:</p>
  494. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">a</span>
  495. </pre></div>
  496. </div>
  497. <p>Referencing the name <code class="docutils literal notranslate"><span class="pre">a</span></code> hereafter is an error (at least until another value
  498. is assigned to it). We’ll find other uses for <a class="reference internal" href="../reference/simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> later.</p>
  499. </section>
  500. <section id="tuples-and-sequences">
  501. <span id="tut-tuples"></span><h2><span class="section-number">5.3. </span>Tuples and Sequences<a class="headerlink" href="#tuples-and-sequences" title="Permalink to this headline">¶</a></h2>
  502. <p>We saw that lists and strings have many common properties, such as indexing and
  503. slicing operations. They are two examples of <em>sequence</em> data types (see
  504. <a class="reference internal" href="../library/stdtypes.html#typesseq"><span class="std std-ref">Sequence Types — list, tuple, range</span></a>). Since Python is an evolving language, other sequence data
  505. types may be added. There is also another standard sequence data type: the
  506. <em>tuple</em>.</p>
  507. <p>A tuple consists of a number of values separated by commas, for instance:</p>
  508. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="mi">12345</span><span class="p">,</span> <span class="mi">54321</span><span class="p">,</span> <span class="s1">&#39;hello!&#39;</span>
  509. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  510. <span class="go">12345</span>
  511. <span class="gp">&gt;&gt;&gt; </span><span class="n">t</span>
  512. <span class="go">(12345, 54321, &#39;hello!&#39;)</span>
  513. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Tuples may be nested:</span>
  514. <span class="gp">... </span><span class="n">u</span> <span class="o">=</span> <span class="n">t</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
  515. <span class="gp">&gt;&gt;&gt; </span><span class="n">u</span>
  516. <span class="go">((12345, 54321, &#39;hello!&#39;), (1, 2, 3, 4, 5))</span>
  517. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Tuples are immutable:</span>
  518. <span class="gp">... </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">88888</span>
  519. <span class="gt">Traceback (most recent call last):</span>
  520. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  521. <span class="gr">TypeError</span>: <span class="n">&#39;tuple&#39; object does not support item assignment</span>
  522. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># but they can contain mutable objects:</span>
  523. <span class="gp">... </span><span class="n">v</span> <span class="o">=</span> <span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>
  524. <span class="gp">&gt;&gt;&gt; </span><span class="n">v</span>
  525. <span class="go">([1, 2, 3], [3, 2, 1])</span>
  526. </pre></div>
  527. </div>
  528. <p>As you see, on output tuples are always enclosed in parentheses, so that nested
  529. tuples are interpreted correctly; they may be input with or without surrounding
  530. parentheses, although often parentheses are necessary anyway (if the tuple is
  531. part of a larger expression). It is not possible to assign to the individual
  532. items of a tuple, however it is possible to create tuples which contain mutable
  533. objects, such as lists.</p>
  534. <p>Though tuples may seem similar to lists, they are often used in different
  535. situations and for different purposes.
  536. Tuples are <a class="reference internal" href="../glossary.html#term-immutable"><span class="xref std std-term">immutable</span></a>, and usually contain a heterogeneous sequence of
  537. elements that are accessed via unpacking (see later in this section) or indexing
  538. (or even by attribute in the case of <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuples</span></code></a>).
  539. Lists are <a class="reference internal" href="../glossary.html#term-mutable"><span class="xref std std-term">mutable</span></a>, and their elements are usually homogeneous and are
  540. accessed by iterating over the list.</p>
  541. <p>A special problem is the construction of tuples containing 0 or 1 items: the
  542. syntax has some extra quirks to accommodate these. Empty tuples are constructed
  543. by an empty pair of parentheses; a tuple with one item is constructed by
  544. following a value with a comma (it is not sufficient to enclose a single value
  545. in parentheses). Ugly, but effective. For example:</p>
  546. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">empty</span> <span class="o">=</span> <span class="p">()</span>
  547. <span class="gp">&gt;&gt;&gt; </span><span class="n">singleton</span> <span class="o">=</span> <span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="c1"># &lt;-- note trailing comma</span>
  548. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">empty</span><span class="p">)</span>
  549. <span class="go">0</span>
  550. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">singleton</span><span class="p">)</span>
  551. <span class="go">1</span>
  552. <span class="gp">&gt;&gt;&gt; </span><span class="n">singleton</span>
  553. <span class="go">(&#39;hello&#39;,)</span>
  554. </pre></div>
  555. </div>
  556. <p>The statement <code class="docutils literal notranslate"><span class="pre">t</span> <span class="pre">=</span> <span class="pre">12345,</span> <span class="pre">54321,</span> <span class="pre">'hello!'</span></code> is an example of <em>tuple packing</em>:
  557. the values <code class="docutils literal notranslate"><span class="pre">12345</span></code>, <code class="docutils literal notranslate"><span class="pre">54321</span></code> and <code class="docutils literal notranslate"><span class="pre">'hello!'</span></code> are packed together in a tuple.
  558. The reverse operation is also possible:</p>
  559. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="n">t</span>
  560. </pre></div>
  561. </div>
  562. <p>This is called, appropriately enough, <em>sequence unpacking</em> and works for any
  563. sequence on the right-hand side. Sequence unpacking requires that there are as
  564. many variables on the left side of the equals sign as there are elements in the
  565. sequence. Note that multiple assignment is really just a combination of tuple
  566. packing and sequence unpacking.</p>
  567. </section>
  568. <section id="sets">
  569. <span id="tut-sets"></span><h2><span class="section-number">5.4. </span>Sets<a class="headerlink" href="#sets" title="Permalink to this headline">¶</a></h2>
  570. <p>Python also includes a data type for <em>sets</em>. A set is an unordered collection
  571. with no duplicate elements. Basic uses include membership testing and
  572. eliminating duplicate entries. Set objects also support mathematical operations
  573. like union, intersection, difference, and symmetric difference.</p>
  574. <p>Curly braces or the <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> function can be used to create sets. Note: to
  575. create an empty set you have to use <code class="docutils literal notranslate"><span class="pre">set()</span></code>, not <code class="docutils literal notranslate"><span class="pre">{}</span></code>; the latter creates an
  576. empty dictionary, a data structure that we discuss in the next section.</p>
  577. <p>Here is a brief demonstration:</p>
  578. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">basket</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;pear&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">}</span>
  579. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">basket</span><span class="p">)</span> <span class="c1"># show that duplicates have been removed</span>
  580. <span class="go">{&#39;orange&#39;, &#39;banana&#39;, &#39;pear&#39;, &#39;apple&#39;}</span>
  581. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;orange&#39;</span> <span class="ow">in</span> <span class="n">basket</span> <span class="c1"># fast membership testing</span>
  582. <span class="go">True</span>
  583. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;crabgrass&#39;</span> <span class="ow">in</span> <span class="n">basket</span>
  584. <span class="go">False</span>
  585. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Demonstrate set operations on unique letters from two words</span>
  586. <span class="gp">...</span>
  587. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;abracadabra&#39;</span><span class="p">)</span>
  588. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s1">&#39;alacazam&#39;</span><span class="p">)</span>
  589. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="c1"># unique letters in a</span>
  590. <span class="go">{&#39;a&#39;, &#39;r&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;}</span>
  591. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="c1"># letters in a but not in b</span>
  592. <span class="go">{&#39;r&#39;, &#39;d&#39;, &#39;b&#39;}</span>
  593. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">|</span> <span class="n">b</span> <span class="c1"># letters in a or b or both</span>
  594. <span class="go">{&#39;a&#39;, &#39;c&#39;, &#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;}</span>
  595. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">&amp;</span> <span class="n">b</span> <span class="c1"># letters in both a and b</span>
  596. <span class="go">{&#39;a&#39;, &#39;c&#39;}</span>
  597. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">^</span> <span class="n">b</span> <span class="c1"># letters in a or b but not both</span>
  598. <span class="go">{&#39;r&#39;, &#39;d&#39;, &#39;b&#39;, &#39;m&#39;, &#39;z&#39;, &#39;l&#39;}</span>
  599. </pre></div>
  600. </div>
  601. <p>Similarly to <a class="reference internal" href="#tut-listcomps"><span class="std std-ref">list comprehensions</span></a>, set comprehensions
  602. are also supported:</p>
  603. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="s1">&#39;abracadabra&#39;</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s1">&#39;abc&#39;</span><span class="p">}</span>
  604. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span>
  605. <span class="go">{&#39;r&#39;, &#39;d&#39;}</span>
  606. </pre></div>
  607. </div>
  608. </section>
  609. <section id="dictionaries">
  610. <span id="tut-dictionaries"></span><h2><span class="section-number">5.5. </span>Dictionaries<a class="headerlink" href="#dictionaries" title="Permalink to this headline">¶</a></h2>
  611. <p>Another useful data type built into Python is the <em>dictionary</em> (see
  612. <a class="reference internal" href="../library/stdtypes.html#typesmapping"><span class="std std-ref">Mapping Types — dict</span></a>). Dictionaries are sometimes found in other languages as
  613. “associative memories” or “associative arrays”. Unlike sequences, which are
  614. indexed by a range of numbers, dictionaries are indexed by <em>keys</em>, which can be
  615. any immutable type; strings and numbers can always be keys. Tuples can be used
  616. as keys if they contain only strings, numbers, or tuples; if a tuple contains
  617. any mutable object either directly or indirectly, it cannot be used as a key.
  618. You can’t use lists as keys, since lists can be modified in place using index
  619. assignments, slice assignments, or methods like <code class="xref py py-meth docutils literal notranslate"><span class="pre">append()</span></code> and
  620. <code class="xref py py-meth docutils literal notranslate"><span class="pre">extend()</span></code>.</p>
  621. <p>It is best to think of a dictionary as a set of <em>key: value</em> pairs,
  622. with the requirement that the keys are unique (within one dictionary). A pair of
  623. braces creates an empty dictionary: <code class="docutils literal notranslate"><span class="pre">{}</span></code>. Placing a comma-separated list of
  624. key:value pairs within the braces adds initial key:value pairs to the
  625. dictionary; this is also the way dictionaries are written on output.</p>
  626. <p>The main operations on a dictionary are storing a value with some key and
  627. extracting the value given the key. It is also possible to delete a key:value
  628. pair with <code class="docutils literal notranslate"><span class="pre">del</span></code>. If you store using a key that is already in use, the old
  629. value associated with that key is forgotten. It is an error to extract a value
  630. using a non-existent key.</p>
  631. <p>Performing <code class="docutils literal notranslate"><span class="pre">list(d)</span></code> on a dictionary returns a list of all the keys
  632. used in the dictionary, in insertion order (if you want it sorted, just use
  633. <code class="docutils literal notranslate"><span class="pre">sorted(d)</span></code> instead). To check whether a single key is in the
  634. dictionary, use the <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> keyword.</p>
  635. <p>Here is a small example using a dictionary:</p>
  636. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;jack&#39;</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s1">&#39;sape&#39;</span><span class="p">:</span> <span class="mi">4139</span><span class="p">}</span>
  637. <span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span><span class="p">[</span><span class="s1">&#39;guido&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4127</span>
  638. <span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span>
  639. <span class="go">{&#39;jack&#39;: 4098, &#39;sape&#39;: 4139, &#39;guido&#39;: 4127}</span>
  640. <span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span><span class="p">[</span><span class="s1">&#39;jack&#39;</span><span class="p">]</span>
  641. <span class="go">4098</span>
  642. <span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">tel</span><span class="p">[</span><span class="s1">&#39;sape&#39;</span><span class="p">]</span>
  643. <span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span><span class="p">[</span><span class="s1">&#39;irv&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4127</span>
  644. <span class="gp">&gt;&gt;&gt; </span><span class="n">tel</span>
  645. <span class="go">{&#39;jack&#39;: 4098, &#39;guido&#39;: 4127, &#39;irv&#39;: 4127}</span>
  646. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">tel</span><span class="p">)</span>
  647. <span class="go">[&#39;jack&#39;, &#39;guido&#39;, &#39;irv&#39;]</span>
  648. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">tel</span><span class="p">)</span>
  649. <span class="go">[&#39;guido&#39;, &#39;irv&#39;, &#39;jack&#39;]</span>
  650. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;guido&#39;</span> <span class="ow">in</span> <span class="n">tel</span>
  651. <span class="go">True</span>
  652. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;jack&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">tel</span>
  653. <span class="go">False</span>
  654. </pre></div>
  655. </div>
  656. <p>The <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-func docutils literal notranslate"><span class="pre">dict()</span></code></a> constructor builds dictionaries directly from sequences of
  657. key-value pairs:</p>
  658. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">dict</span><span class="p">([(</span><span class="s1">&#39;sape&#39;</span><span class="p">,</span> <span class="mi">4139</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;guido&#39;</span><span class="p">,</span> <span class="mi">4127</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;jack&#39;</span><span class="p">,</span> <span class="mi">4098</span><span class="p">)])</span>
  659. <span class="go">{&#39;sape&#39;: 4139, &#39;guido&#39;: 4127, &#39;jack&#39;: 4098}</span>
  660. </pre></div>
  661. </div>
  662. <p>In addition, dict comprehensions can be used to create dictionaries from
  663. arbitrary key and value expressions:</p>
  664. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">)}</span>
  665. <span class="go">{2: 4, 4: 16, 6: 36}</span>
  666. </pre></div>
  667. </div>
  668. <p>When the keys are simple strings, it is sometimes easier to specify pairs using
  669. keyword arguments:</p>
  670. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">dict</span><span class="p">(</span><span class="n">sape</span><span class="o">=</span><span class="mi">4139</span><span class="p">,</span> <span class="n">guido</span><span class="o">=</span><span class="mi">4127</span><span class="p">,</span> <span class="n">jack</span><span class="o">=</span><span class="mi">4098</span><span class="p">)</span>
  671. <span class="go">{&#39;sape&#39;: 4139, &#39;guido&#39;: 4127, &#39;jack&#39;: 4098}</span>
  672. </pre></div>
  673. </div>
  674. </section>
  675. <section id="looping-techniques">
  676. <span id="tut-loopidioms"></span><h2><span class="section-number">5.6. </span>Looping Techniques<a class="headerlink" href="#looping-techniques" title="Permalink to this headline">¶</a></h2>
  677. <p>When looping through dictionaries, the key and corresponding value can be
  678. retrieved at the same time using the <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code></a> method.</p>
  679. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">knights</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;gallahad&#39;</span><span class="p">:</span> <span class="s1">&#39;the pure&#39;</span><span class="p">,</span> <span class="s1">&#39;robin&#39;</span><span class="p">:</span> <span class="s1">&#39;the brave&#39;</span><span class="p">}</span>
  680. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">knights</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
  681. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
  682. <span class="gp">...</span>
  683. <span class="go">gallahad the pure</span>
  684. <span class="go">robin the brave</span>
  685. </pre></div>
  686. </div>
  687. <p>When looping through a sequence, the position index and corresponding value can
  688. be retrieved at the same time using the <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><code class="xref py py-func docutils literal notranslate"><span class="pre">enumerate()</span></code></a> function.</p>
  689. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">([</span><span class="s1">&#39;tic&#39;</span><span class="p">,</span> <span class="s1">&#39;tac&#39;</span><span class="p">,</span> <span class="s1">&#39;toe&#39;</span><span class="p">]):</span>
  690. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
  691. <span class="gp">...</span>
  692. <span class="go">0 tic</span>
  693. <span class="go">1 tac</span>
  694. <span class="go">2 toe</span>
  695. </pre></div>
  696. </div>
  697. <p>To loop over two or more sequences at the same time, the entries can be paired
  698. with the <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> function.</p>
  699. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">questions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;quest&#39;</span><span class="p">,</span> <span class="s1">&#39;favorite color&#39;</span><span class="p">]</span>
  700. <span class="gp">&gt;&gt;&gt; </span><span class="n">answers</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;lancelot&#39;</span><span class="p">,</span> <span class="s1">&#39;the holy grail&#39;</span><span class="p">,</span> <span class="s1">&#39;blue&#39;</span><span class="p">]</span>
  701. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">q</span><span class="p">,</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">questions</span><span class="p">,</span> <span class="n">answers</span><span class="p">):</span>
  702. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;What is your </span><span class="si">{0}</span><span class="s1">? It is </span><span class="si">{1}</span><span class="s1">.&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">a</span><span class="p">))</span>
  703. <span class="gp">...</span>
  704. <span class="go">What is your name? It is lancelot.</span>
  705. <span class="go">What is your quest? It is the holy grail.</span>
  706. <span class="go">What is your favorite color? It is blue.</span>
  707. </pre></div>
  708. </div>
  709. <p>To loop over a sequence in reverse, first specify the sequence in a forward
  710. direction and then call the <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> function.</p>
  711. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)):</span>
  712. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
  713. <span class="gp">...</span>
  714. <span class="go">9</span>
  715. <span class="go">7</span>
  716. <span class="go">5</span>
  717. <span class="go">3</span>
  718. <span class="go">1</span>
  719. </pre></div>
  720. </div>
  721. <p>To loop over a sequence in sorted order, use the <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a> function which
  722. returns a new sorted list while leaving the source unaltered.</p>
  723. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">basket</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;pear&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">]</span>
  724. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">basket</span><span class="p">):</span>
  725. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
  726. <span class="gp">...</span>
  727. <span class="go">apple</span>
  728. <span class="go">apple</span>
  729. <span class="go">banana</span>
  730. <span class="go">orange</span>
  731. <span class="go">orange</span>
  732. <span class="go">pear</span>
  733. </pre></div>
  734. </div>
  735. <p>Using <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> on a sequence eliminates duplicate elements. The use of
  736. <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a> in combination with <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> over a sequence is an idiomatic
  737. way to loop over unique elements of the sequence in sorted order.</p>
  738. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">basket</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;apple&#39;</span><span class="p">,</span> <span class="s1">&#39;pear&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;banana&#39;</span><span class="p">]</span>
  739. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">basket</span><span class="p">)):</span>
  740. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  741. <span class="gp">...</span>
  742. <span class="go">apple</span>
  743. <span class="go">banana</span>
  744. <span class="go">orange</span>
  745. <span class="go">pear</span>
  746. </pre></div>
  747. </div>
  748. <p>It is sometimes tempting to change a list while you are looping over it;
  749. however, it is often simpler and safer to create a new list instead.</p>
  750. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
  751. <span class="gp">&gt;&gt;&gt; </span><span class="n">raw_data</span> <span class="o">=</span> <span class="p">[</span><span class="mf">56.2</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;NaN&#39;</span><span class="p">),</span> <span class="mf">51.7</span><span class="p">,</span> <span class="mf">55.3</span><span class="p">,</span> <span class="mf">52.5</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;NaN&#39;</span><span class="p">),</span> <span class="mf">47.8</span><span class="p">]</span>
  752. <span class="gp">&gt;&gt;&gt; </span><span class="n">filtered_data</span> <span class="o">=</span> <span class="p">[]</span>
  753. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">raw_data</span><span class="p">:</span>
  754. <span class="gp">... </span> <span class="k">if</span> <span class="ow">not</span> <span class="n">math</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
  755. <span class="gp">... </span> <span class="n">filtered_data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
  756. <span class="gp">...</span>
  757. <span class="gp">&gt;&gt;&gt; </span><span class="n">filtered_data</span>
  758. <span class="go">[56.2, 51.7, 55.3, 52.5, 47.8]</span>
  759. </pre></div>
  760. </div>
  761. </section>
  762. <section id="more-on-conditions">
  763. <span id="tut-conditions"></span><h2><span class="section-number">5.7. </span>More on Conditions<a class="headerlink" href="#more-on-conditions" title="Permalink to this headline">¶</a></h2>
  764. <p>The conditions used in <code class="docutils literal notranslate"><span class="pre">while</span></code> and <code class="docutils literal notranslate"><span class="pre">if</span></code> statements can contain any
  765. operators, not just comparisons.</p>
  766. <p>The comparison operators <code class="docutils literal notranslate"><span class="pre">in</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code> are membership tests that
  767. determine whether a value is in (or not in) a container. The operators <code class="docutils literal notranslate"><span class="pre">is</span></code>
  768. and <code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code> compare whether two objects are really the same object. All
  769. comparison operators have the same priority, which is lower than that of all
  770. numerical operators.</p>
  771. <p>Comparisons can be chained. For example, <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span> <span class="pre">==</span> <span class="pre">c</span></code> tests whether <code class="docutils literal notranslate"><span class="pre">a</span></code> is
  772. less than <code class="docutils literal notranslate"><span class="pre">b</span></code> and moreover <code class="docutils literal notranslate"><span class="pre">b</span></code> equals <code class="docutils literal notranslate"><span class="pre">c</span></code>.</p>
  773. <p>Comparisons may be combined using the Boolean operators <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code>, and
  774. the outcome of a comparison (or of any other Boolean expression) may be negated
  775. with <code class="docutils literal notranslate"><span class="pre">not</span></code>. These have lower priorities than comparison operators; between
  776. them, <code class="docutils literal notranslate"><span class="pre">not</span></code> has the highest priority and <code class="docutils literal notranslate"><span class="pre">or</span></code> the lowest, so that <code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre">and</span>
  777. <span class="pre">not</span> <span class="pre">B</span> <span class="pre">or</span> <span class="pre">C</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">(A</span> <span class="pre">and</span> <span class="pre">(not</span> <span class="pre">B))</span> <span class="pre">or</span> <span class="pre">C</span></code>. As always, parentheses
  778. can be used to express the desired composition.</p>
  779. <p>The Boolean operators <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code> are so-called <em>short-circuit</em>
  780. operators: their arguments are evaluated from left to right, and evaluation
  781. stops as soon as the outcome is determined. For example, if <code class="docutils literal notranslate"><span class="pre">A</span></code> and <code class="docutils literal notranslate"><span class="pre">C</span></code> are
  782. true but <code class="docutils literal notranslate"><span class="pre">B</span></code> is false, <code class="docutils literal notranslate"><span class="pre">A</span> <span class="pre">and</span> <span class="pre">B</span> <span class="pre">and</span> <span class="pre">C</span></code> does not evaluate the expression
  783. <code class="docutils literal notranslate"><span class="pre">C</span></code>. When used as a general value and not as a Boolean, the return value of a
  784. short-circuit operator is the last evaluated argument.</p>
  785. <p>It is possible to assign the result of a comparison or other Boolean expression
  786. to a variable. For example,</p>
  787. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">string1</span><span class="p">,</span> <span class="n">string2</span><span class="p">,</span> <span class="n">string3</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;Trondheim&#39;</span><span class="p">,</span> <span class="s1">&#39;Hammer Dance&#39;</span>
  788. <span class="gp">&gt;&gt;&gt; </span><span class="n">non_null</span> <span class="o">=</span> <span class="n">string1</span> <span class="ow">or</span> <span class="n">string2</span> <span class="ow">or</span> <span class="n">string3</span>
  789. <span class="gp">&gt;&gt;&gt; </span><span class="n">non_null</span>
  790. <span class="go">&#39;Trondheim&#39;</span>
  791. </pre></div>
  792. </div>
  793. <p>Note that in Python, unlike C, assignment inside expressions must be done
  794. explicitly with the
  795. <a class="reference internal" href="../faq/design.html#why-can-t-i-use-an-assignment-in-an-expression"><span class="std std-ref">walrus operator</span></a> <code class="docutils literal notranslate"><span class="pre">:=</span></code>.
  796. This avoids a common class of problems encountered in C programs: typing <code class="docutils literal notranslate"><span class="pre">=</span></code>
  797. in an expression when <code class="docutils literal notranslate"><span class="pre">==</span></code> was intended.</p>
  798. </section>
  799. <section id="comparing-sequences-and-other-types">
  800. <span id="tut-comparing"></span><h2><span class="section-number">5.8. </span>Comparing Sequences and Other Types<a class="headerlink" href="#comparing-sequences-and-other-types" title="Permalink to this headline">¶</a></h2>
  801. <p>Sequence objects typically may be compared to other objects with the same sequence
  802. type. The comparison uses <em>lexicographical</em> ordering: first the first two
  803. items are compared, and if they differ this determines the outcome of the
  804. comparison; if they are equal, the next two items are compared, and so on, until
  805. either sequence is exhausted. If two items to be compared are themselves
  806. sequences of the same type, the lexicographical comparison is carried out
  807. recursively. If all items of two sequences compare equal, the sequences are
  808. considered equal. If one sequence is an initial sub-sequence of the other, the
  809. shorter sequence is the smaller (lesser) one. Lexicographical ordering for
  810. strings uses the Unicode code point number to order individual characters.
  811. Some examples of comparisons between sequences of the same type:</p>
  812. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
  813. <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="o">&lt;</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
  814. <span class="s1">&#39;ABC&#39;</span> <span class="o">&lt;</span> <span class="s1">&#39;C&#39;</span> <span class="o">&lt;</span> <span class="s1">&#39;Pascal&#39;</span> <span class="o">&lt;</span> <span class="s1">&#39;Python&#39;</span>
  815. <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
  816. <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
  817. <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">)</span>
  818. <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;aa&#39;</span><span class="p">,</span> <span class="s1">&#39;ab&#39;</span><span class="p">))</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span>
  819. </pre></div>
  820. </div>
  821. <p>Note that comparing objects of different types with <code class="docutils literal notranslate"><span class="pre">&lt;</span></code> or <code class="docutils literal notranslate"><span class="pre">&gt;</span></code> is legal
  822. provided that the objects have appropriate comparison methods. For example,
  823. mixed numeric types are compared according to their numeric value, so 0 equals
  824. 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the
  825. interpreter will raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception.</p>
  826. <p class="rubric">Footnotes</p>
  827. <dl class="footnote brackets">
  828. <dt class="label" id="id2"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
  829. <dd><p>Other languages may return the mutated object, which allows method
  830. chaining, such as <code class="docutils literal notranslate"><span class="pre">d-&gt;insert(&quot;a&quot;)-&gt;remove(&quot;b&quot;)-&gt;sort();</span></code>.</p>
  831. </dd>
  832. </dl>
  833. </section>
  834. </section>
  835. <div class="clearer"></div>
  836. </div>
  837. </div>
  838. </div>
  839. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  840. <div class="sphinxsidebarwrapper">
  841. <div>
  842. <h3><a href="../contents.html">Table of Contents</a></h3>
  843. <ul>
  844. <li><a class="reference internal" href="#">5. Data Structures</a><ul>
  845. <li><a class="reference internal" href="#more-on-lists">5.1. More on Lists</a><ul>
  846. <li><a class="reference internal" href="#using-lists-as-stacks">5.1.1. Using Lists as Stacks</a></li>
  847. <li><a class="reference internal" href="#using-lists-as-queues">5.1.2. Using Lists as Queues</a></li>
  848. <li><a class="reference internal" href="#list-comprehensions">5.1.3. List Comprehensions</a></li>
  849. <li><a class="reference internal" href="#nested-list-comprehensions">5.1.4. Nested List Comprehensions</a></li>
  850. </ul>
  851. </li>
  852. <li><a class="reference internal" href="#the-del-statement">5.2. The <code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code> statement</a></li>
  853. <li><a class="reference internal" href="#tuples-and-sequences">5.3. Tuples and Sequences</a></li>
  854. <li><a class="reference internal" href="#sets">5.4. Sets</a></li>
  855. <li><a class="reference internal" href="#dictionaries">5.5. Dictionaries</a></li>
  856. <li><a class="reference internal" href="#looping-techniques">5.6. Looping Techniques</a></li>
  857. <li><a class="reference internal" href="#more-on-conditions">5.7. More on Conditions</a></li>
  858. <li><a class="reference internal" href="#comparing-sequences-and-other-types">5.8. Comparing Sequences and Other Types</a></li>
  859. </ul>
  860. </li>
  861. </ul>
  862. </div>
  863. <div>
  864. <h4>Previous topic</h4>
  865. <p class="topless"><a href="controlflow.html"
  866. title="previous chapter"><span class="section-number">4. </span>More Control Flow Tools</a></p>
  867. </div>
  868. <div>
  869. <h4>Next topic</h4>
  870. <p class="topless"><a href="modules.html"
  871. title="next chapter"><span class="section-number">6. </span>Modules</a></p>
  872. </div>
  873. <div role="note" aria-label="source link">
  874. <h3>This Page</h3>
  875. <ul class="this-page-menu">
  876. <li><a href="../bugs.html">Report a Bug</a></li>
  877. <li>
  878. <a href="https://github.com/python/cpython/blob/main/Doc/tutorial/datastructures.rst"
  879. rel="nofollow">Show Source
  880. </a>
  881. </li>
  882. </ul>
  883. </div>
  884. </div>
  885. </div>
  886. <div class="clearer"></div>
  887. </div>
  888. <div class="related" role="navigation" aria-label="related navigation">
  889. <h3>Navigation</h3>
  890. <ul>
  891. <li class="right" style="margin-right: 10px">
  892. <a href="../genindex.html" title="General Index"
  893. >index</a></li>
  894. <li class="right" >
  895. <a href="../py-modindex.html" title="Python Module Index"
  896. >modules</a> |</li>
  897. <li class="right" >
  898. <a href="modules.html" title="6. Modules"
  899. >next</a> |</li>
  900. <li class="right" >
  901. <a href="controlflow.html" title="4. More Control Flow Tools"
  902. >previous</a> |</li>
  903. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  904. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  905. <li class="switchers">
  906. <div class="language_switcher_placeholder"></div>
  907. <div class="version_switcher_placeholder"></div>
  908. </li>
  909. <li>
  910. </li>
  911. <li id="cpython-language-and-version">
  912. <a href="../index.html">3.12.0 Documentation</a> &#187;
  913. </li>
  914. <li class="nav-item nav-item-1"><a href="index.html" >The Python Tutorial</a> &#187;</li>
  915. <li class="nav-item nav-item-this"><a href=""><span class="section-number">5. </span>Data Structures</a></li>
  916. <li class="right">
  917. <div class="inline-search" role="search">
  918. <form class="inline-search" action="../search.html" method="get">
  919. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  920. <input type="submit" value="Go" />
  921. </form>
  922. </div>
  923. |
  924. </li>
  925. <li class="right">
  926. <label class="theme-selector-label">
  927. Theme
  928. <select class="theme-selector" oninput="activateTheme(this.value)">
  929. <option value="auto" selected>Auto</option>
  930. <option value="light">Light</option>
  931. <option value="dark">Dark</option>
  932. </select>
  933. </label> |</li>
  934. </ul>
  935. </div>
  936. <div class="footer">
  937. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  938. <br />
  939. This page is licensed under the Python Software Foundation License Version 2.
  940. <br />
  941. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  942. <br />
  943. See <a href="/license.html">History and License</a> for more information.<br />
  944. <br />
  945. The Python Software Foundation is a non-profit corporation.
  946. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  947. <br />
  948. <br />
  949. Last updated on Oct 02, 2023.
  950. <a href="/bugs.html">Found a bug</a>?
  951. <br />
  952. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  953. </div>
  954. </body>
  955. </html>