Linux server.nvwebsoft.co.in 3.10.0-1160.114.2.el7.x86_64 #1 SMP Wed Mar 20 15:54:52 UTC 2024 x86_64
Apache
: 162.240.12.249 | : 3.144.254.149
202 Domain
8.1.31
nbspublicschool
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
tutorial /
[ HOME SHELL ]
Name
Size
Permission
Action
appetite.html
10.28
KB
-rw-r--r--
classes.html
77.01
KB
-rw-r--r--
controlflow.html
69.18
KB
-rw-r--r--
datastructures.html
82.82
KB
-rw-r--r--
errors.html
45.4
KB
-rw-r--r--
floatingpoint.html
20.59
KB
-rw-r--r--
index.html
24.28
KB
-rw-r--r--
inputoutput.html
48.57
KB
-rw-r--r--
interactive.html
17.99
KB
-rw-r--r--
interpreter.html
26.6
KB
-rw-r--r--
introduction.html
58.9
KB
-rw-r--r--
modules.html
55.63
KB
-rw-r--r--
stdlib.html
40.52
KB
-rw-r--r--
stdlib2.html
53.09
KB
-rw-r--r--
whatnow.html
9.69
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : datastructures.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>5. Data Structures — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="The Python Tutorial" href="index.html" /> <link rel="next" title="6. Modules" href="modules.html" /> <link rel="prev" title="4. More Control Flow Tools" href="controlflow.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="modules.html" title="6. Modules" accesskey="N">next</a> |</li> <li class="right" > <a href="controlflow.html" title="4. More Control Flow Tools" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">The Python Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="data-structures"> <span id="tut-structures"></span><h1>5. Data Structures<a class="headerlink" href="#data-structures" title="Permalink to this headline">¶</a></h1> <p>This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.</p> <div class="section" id="more-on-lists"> <span id="tut-morelists"></span><h2>5.1. More on Lists<a class="headerlink" href="#more-on-lists" title="Permalink to this headline">¶</a></h2> <p>The list data type has some more methods. Here are all of the methods of list objects:</p> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">append</tt><big>(</big><em>x</em><big>)</big></dt> <dd><p>Add an item to the end of the list; equivalent to <tt class="docutils literal"><span class="pre">a[len(a):]</span> <span class="pre">=</span> <span class="pre">[x]</span></tt>.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">extend</tt><big>(</big><em>L</em><big>)</big></dt> <dd><p>Extend the list by appending all the items in the given list; equivalent to <tt class="docutils literal"><span class="pre">a[len(a):]</span> <span class="pre">=</span> <span class="pre">L</span></tt>.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">insert</tt><big>(</big><em>i</em>, <em>x</em><big>)</big></dt> <dd><p>Insert an item at a given position. The first argument is the index of the element before which to insert, so <tt class="docutils literal"><span class="pre">a.insert(0,</span> <span class="pre">x)</span></tt> inserts at the front of the list, and <tt class="docutils literal"><span class="pre">a.insert(len(a),</span> <span class="pre">x)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">a.append(x)</span></tt>.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">remove</tt><big>(</big><em>x</em><big>)</big></dt> <dd><p>Remove the first item from the list whose value is <em>x</em>. It is an error if there is no such item.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">pop</tt><big>(</big><span class="optional">[</span><em>i</em><span class="optional">]</span><big>)</big></dt> <dd><p>Remove the item at the given position in the list, and return it. If no index is specified, <tt class="docutils literal"><span class="pre">a.pop()</span></tt> removes and returns the last item in the list. (The square brackets around the <em>i</em> in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">index</tt><big>(</big><em>x</em><big>)</big></dt> <dd><p>Return the index in the list of the first item whose value is <em>x</em>. It is an error if there is no such item.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">count</tt><big>(</big><em>x</em><big>)</big></dt> <dd><p>Return the number of times <em>x</em> appears in the list.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">sort</tt><big>(</big><big>)</big></dt> <dd><p>Sort the items of the list, in place.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">list.</tt><tt class="descname">reverse</tt><big>(</big><big>)</big></dt> <dd><p>Reverse the elements of the list, in place.</p> </dd></dl> <p>An example that uses most of the list methods:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</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="mi">1</span><span class="p">,</span> <span class="mf">1234.5</span><span class="p">]</span> <span class="gp">>>> </span><span class="k">print</span> <span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">333</span><span class="p">),</span> <span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mf">66.25</span><span class="p">),</span> <span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s">'x'</span><span class="p">)</span> <span class="go">2 1 0</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">insert</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> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">333</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[66.25, 333, -1, 333, 1, 1234.5, 333]</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="mi">333</span><span class="p">)</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="mi">333</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[66.25, -1, 333, 1, 1234.5, 333]</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[333, 1234.5, 1, 333, -1, 66.25]</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[-1, 1, 66.25, 333, 333, 1234.5]</span> </pre></div> </div> <div class="section" id="using-lists-as-stacks"> <span id="tut-lists-as-stacks"></span><h3>5.1.1. Using Lists as Stacks<a class="headerlink" href="#using-lists-as-stacks" title="Permalink to this headline">¶</a></h3> <p>The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use <tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt>. To retrieve an item from the top of the stack, use <tt class="xref py py-meth docutils literal"><span class="pre">pop()</span></tt> without an explicit index. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="gp">>>> </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> <span class="gp">>>> </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> <span class="gp">>>> </span><span class="n">stack</span> <span class="go">[3, 4, 5, 6, 7]</span> <span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="go">7</span> <span class="gp">>>> </span><span class="n">stack</span> <span class="go">[3, 4, 5, 6]</span> <span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="go">6</span> <span class="gp">>>> </span><span class="n">stack</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span> <span class="go">5</span> <span class="gp">>>> </span><span class="n">stack</span> <span class="go">[3, 4]</span> </pre></div> </div> </div> <div class="section" id="using-lists-as-queues"> <span id="tut-lists-as-queues"></span><h3>5.1.2. Using Lists as Queues<a class="headerlink" href="#using-lists-as-queues" title="Permalink to this headline">¶</a></h3> <p>It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).</p> <p>To implement a queue, use <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">collections.deque</span></tt></a> which was designed to have fast appends and pops from both ends. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span> <span class="gp">>>> </span><span class="n">queue</span> <span class="o">=</span> <span class="n">deque</span><span class="p">([</span><span class="s">"Eric"</span><span class="p">,</span> <span class="s">"John"</span><span class="p">,</span> <span class="s">"Michael"</span><span class="p">])</span> <span class="gp">>>> </span><span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">"Terry"</span><span class="p">)</span> <span class="c"># Terry arrives</span> <span class="gp">>>> </span><span class="n">queue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">"Graham"</span><span class="p">)</span> <span class="c"># Graham arrives</span> <span class="gp">>>> </span><span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c"># The first to arrive now leaves</span> <span class="go">'Eric'</span> <span class="gp">>>> </span><span class="n">queue</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="c"># The second to arrive now leaves</span> <span class="go">'John'</span> <span class="gp">>>> </span><span class="n">queue</span> <span class="c"># Remaining queue in order of arrival</span> <span class="go">deque(['Michael', 'Terry', 'Graham'])</span> </pre></div> </div> </div> <div class="section" id="functional-programming-tools"> <span id="tut-functional"></span><h3>5.1.3. Functional Programming Tools<a class="headerlink" href="#functional-programming-tools" title="Permalink to this headline">¶</a></h3> <p>There are three built-in functions that are very useful when used with lists: <a class="reference internal" href="../library/functions.html#filter" title="filter"><tt class="xref py py-func docutils literal"><span class="pre">filter()</span></tt></a>, <a class="reference internal" href="../library/functions.html#map" title="map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a>, and <a class="reference internal" href="../library/functions.html#reduce" title="reduce"><tt class="xref py py-func docutils literal"><span class="pre">reduce()</span></tt></a>.</p> <p><tt class="docutils literal"><span class="pre">filter(function,</span> <span class="pre">sequence)</span></tt> returns a sequence consisting of those items from the sequence for which <tt class="docutils literal"><span class="pre">function(item)</span></tt> is true. If <em>sequence</em> is a <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><tt class="xref py py-class docutils literal"><span class="pre">string</span></tt></a> or <a class="reference internal" href="../library/functions.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>, the result will be of the same type; otherwise, it is always a <a class="reference internal" href="../library/functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>. For example, to compute a sequence of numbers not divisible by 2 and 3:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">!=</span> <span class="mi">0</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">filter</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">25</span><span class="p">))</span> <span class="go">[5, 7, 11, 13, 17, 19, 23]</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">map(function,</span> <span class="pre">sequence)</span></tt> calls <tt class="docutils literal"><span class="pre">function(item)</span></tt> for each of the sequence’s items and returns a list of the return values. For example, to compute some cubes:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">cube</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">map</span><span class="p">(</span><span class="n">cube</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">11</span><span class="p">))</span> <span class="go">[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]</span> </pre></div> </div> <p>More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or <tt class="docutils literal"><span class="pre">None</span></tt> if some sequence is shorter than another). For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">seq</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">add</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">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">map</span><span class="p">(</span><span class="n">add</span><span class="p">,</span> <span class="n">seq</span><span class="p">,</span> <span class="n">seq</span><span class="p">)</span> <span class="go">[0, 2, 4, 6, 8, 10, 12, 14]</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">reduce(function,</span> <span class="pre">sequence)</span></tt> returns a single value constructed by calling the binary function <em>function</em> on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">add</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">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">reduce</span><span class="p">(</span><span class="n">add</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">11</span><span class="p">))</span> <span class="go">55</span> </pre></div> </div> <p>If there’s only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.</p> <p>A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">def</span> <span class="nf">sum</span><span class="p">(</span><span class="n">seq</span><span class="p">):</span> <span class="gp">... </span> <span class="k">def</span> <span class="nf">add</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">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span> <span class="gp">... </span> <span class="k">return</span> <span class="nb">reduce</span><span class="p">(</span><span class="n">add</span><span class="p">,</span> <span class="n">seq</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="nb">sum</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">11</span><span class="p">))</span> <span class="go">55</span> <span class="gp">>>> </span><span class="nb">sum</span><span class="p">([])</span> <span class="go">0</span> </pre></div> </div> <p>Don’t use this example’s definition of <a class="reference internal" href="../library/functions.html#sum" title="sum"><tt class="xref py py-func docutils literal"><span class="pre">sum()</span></tt></a>: since summing numbers is such a common need, a built-in function <tt class="docutils literal"><span class="pre">sum(sequence)</span></tt> is already provided, and works exactly like this.</p> </div> <div class="section" id="list-comprehensions"> <span id="tut-listcomps"></span><h3>5.1.4. List Comprehensions<a class="headerlink" href="#list-comprehensions" title="Permalink to this headline">¶</a></h3> <p>List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.</p> <p>For example, assume we want to create a list of squares, like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">squares</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </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> <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> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">squares</span> <span class="go">[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span> </pre></div> </div> <p>We can obtain the same result with:</p> <div class="highlight-python"><div class="highlight"><pre><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> </pre></div> </div> <p>This is also equivalent to <tt class="docutils literal"><span class="pre">squares</span> <span class="pre">=</span> <span class="pre">map(lambda</span> <span class="pre">x:</span> <span class="pre">x**2,</span> <span class="pre">range(10))</span></tt>, but it’s more concise and readable.</p> <p>A list comprehension consists of brackets containing an expression followed by a <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> clause, then zero or more <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> or <a class="reference internal" href="../reference/compound_stmts.html#if"><tt class="xref std std-keyword docutils literal"><span class="pre">if</span></tt></a> clauses. The result will be a new list resulting from evaluating the expression in the context of the <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> and <a class="reference internal" href="../reference/compound_stmts.html#if"><tt class="xref std std-keyword docutils literal"><span class="pre">if</span></tt></a> clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="go">[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span> </pre></div> </div> <p>and it’s equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">combs</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </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="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> <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> <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> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">combs</span> <span class="go">[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]</span> </pre></div> </div> <p>Note how the order of the <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> and <a class="reference internal" href="../reference/compound_stmts.html#if"><tt class="xref std std-keyword docutils literal"><span class="pre">if</span></tt></a> statements is the same in both these snippets.</p> <p>If the expression is a tuple (e.g. the <tt class="docutils literal"><span class="pre">(x,</span> <span class="pre">y)</span></tt> in the previous example), it must be parenthesized.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="gp">>>> </span><span class="c"># create a new list with the values doubled</span> <span class="gp">>>> </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> <span class="go">[-8, -4, 0, 4, 8]</span> <span class="gp">>>> </span><span class="c"># filter the list to exclude negative numbers</span> <span class="gp">>>> </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">>=</span> <span class="mi">0</span><span class="p">]</span> <span class="go">[0, 2, 4]</span> <span class="gp">>>> </span><span class="c"># apply a function to all the elements</span> <span class="gp">>>> </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> <span class="go">[4, 2, 0, 2, 4]</span> <span class="gp">>>> </span><span class="c"># call a method on each element</span> <span class="gp">>>> </span><span class="n">freshfruit</span> <span class="o">=</span> <span class="p">[</span><span class="s">' banana'</span><span class="p">,</span> <span class="s">' loganberry '</span><span class="p">,</span> <span class="s">'passion fruit '</span><span class="p">]</span> <span class="gp">>>> </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> <span class="go">['banana', 'loganberry', 'passion fruit']</span> <span class="gp">>>> </span><span class="c"># create a list of 2-tuples like (number, square)</span> <span class="gp">>>> </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> <span class="go">[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]</span> <span class="gp">>>> </span><span class="c"># the tuple must be parenthesized, otherwise an error is raised</span> <span class="gp">>>> </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> File <span class="nb">"<stdin>"</span>, line <span class="m">1</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> <span class="o">^</span> <span class="gr">SyntaxError</span>: <span class="n">invalid syntax</span> <span class="gp">>>> </span><span class="c"># flatten a list using a listcomp with two 'for'</span> <span class="gp">>>> </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> <span class="gp">>>> </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> <span class="go">[1, 2, 3, 4, 5, 6, 7, 8, 9]</span> </pre></div> </div> <p>List comprehensions can contain complex expressions and nested functions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">pi</span> <span class="gp">>>> </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> <span class="go">['3.1', '3.14', '3.142', '3.1416', '3.14159']</span> </pre></div> </div> <div class="section" id="nested-list-comprehensions"> <h4>5.1.4.1. Nested List Comprehensions<a class="headerlink" href="#nested-list-comprehensions" title="Permalink to this headline">¶</a></h4> <p>The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.</p> <p>Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">matrix</span> <span class="o">=</span> <span class="p">[</span> <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> <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> <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> <span class="gp">... </span><span class="p">]</span> </pre></div> </div> <p>The following list comprehension will transpose rows and columns:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span> </pre></div> </div> <p>As we saw in the previous section, the nested listcomp is evaluated in the context of the <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> that follows it, so this example is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">transposed</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </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> <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> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">transposed</span> <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span> </pre></div> </div> <p>which, in turn, is the same as:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">transposed</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </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> <span class="gp">... </span> <span class="c"># the following 3 lines implement the nested listcomp</span> <span class="gp">... </span> <span class="n">transposed_row</span> <span class="o">=</span> <span class="p">[]</span> <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> <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> <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> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">transposed</span> <span class="go">[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span> </pre></div> </div> <p>In the real world, you should prefer built-in functions to complex flow statements. The <a class="reference internal" href="../library/functions.html#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a> function would do a great job for this use case:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="n">matrix</span><span class="p">)</span> <span class="go">[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]</span> </pre></div> </div> <p>See <a class="reference internal" href="controlflow.html#tut-unpacking-arguments"><em>Unpacking Argument Lists</em></a> for details on the asterisk in this line.</p> </div> </div> </div> <div class="section" id="the-del-statement"> <span id="tut-del"></span><h2>5.2. The <a class="reference internal" href="../reference/simple_stmts.html#del"><tt class="xref std std-keyword docutils literal"><span class="pre">del</span></tt></a> statement<a class="headerlink" href="#the-del-statement" title="Permalink to this headline">¶</a></h2> <p>There is a way to remove an item from a list given its index instead of its value: the <a class="reference internal" href="../reference/simple_stmts.html#del"><tt class="xref std std-keyword docutils literal"><span class="pre">del</span></tt></a> statement. This differs from the <tt class="xref py py-meth docutils literal"><span class="pre">pop()</span></tt> method which returns a value. The <a class="reference internal" href="../reference/simple_stmts.html#del"><tt class="xref std std-keyword docutils literal"><span class="pre">del</span></tt></a> statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice). For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="gp">>>> </span><span class="k">del</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[1, 66.25, 333, 333, 1234.5]</span> <span class="gp">>>> </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> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[1, 66.25, 1234.5]</span> <span class="gp">>>> </span><span class="k">del</span> <span class="n">a</span><span class="p">[:]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[]</span> </pre></div> </div> <p><a class="reference internal" href="../reference/simple_stmts.html#del"><tt class="xref std std-keyword docutils literal"><span class="pre">del</span></tt></a> can also be used to delete entire variables:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">del</span> <span class="n">a</span> </pre></div> </div> <p>Referencing the name <tt class="docutils literal"><span class="pre">a</span></tt> hereafter is an error (at least until another value is assigned to it). We’ll find other uses for <a class="reference internal" href="../reference/simple_stmts.html#del"><tt class="xref std std-keyword docutils literal"><span class="pre">del</span></tt></a> later.</p> </div> <div class="section" id="tuples-and-sequences"> <span id="tut-tuples"></span><h2>5.3. Tuples and Sequences<a class="headerlink" href="#tuples-and-sequences" title="Permalink to this headline">¶</a></h2> <p>We saw that lists and strings have many common properties, such as indexing and slicing operations. They are two examples of <em>sequence</em> data types (see <a class="reference internal" href="../library/stdtypes.html#typesseq"><em>Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</em></a>). Since Python is an evolving language, other sequence data types may be added. There is also another standard sequence data type: the <em>tuple</em>.</p> <p>A tuple consists of a number of values separated by commas, for instance:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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="s">'hello!'</span> <span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="go">12345</span> <span class="gp">>>> </span><span class="n">t</span> <span class="go">(12345, 54321, 'hello!')</span> <span class="gp">>>> </span><span class="c"># Tuples may be nested:</span> <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> <span class="gp">>>> </span><span class="n">u</span> <span class="go">((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))</span> <span class="gp">>>> </span><span class="c"># Tuples are immutable:</span> <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> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span> <span class="gr">TypeError</span>: <span class="n">'tuple' object does not support item assignment</span> <span class="gp">>>> </span><span class="c"># but they can contain mutable objects:</span> <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> <span class="gp">>>> </span><span class="n">v</span> <span class="go">([1, 2, 3], [3, 2, 1])</span> </pre></div> </div> <p>As you see, on output tuples are always enclosed in parentheses, so that nested tuples are interpreted correctly; they may be input with or without surrounding parentheses, although often parentheses are necessary anyway (if the tuple is part of a larger expression). It is not possible to assign to the individual items of a tuple, however it is possible to create tuples which contain mutable objects, such as lists.</p> <p>Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are <a class="reference internal" href="../glossary.html#term-immutable"><em class="xref std std-term">immutable</em></a>, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-func docutils literal"><span class="pre">namedtuples</span></tt></a>). Lists are <a class="reference internal" href="../glossary.html#term-mutable"><em class="xref std std-term">mutable</em></a>, and their elements are usually homogeneous and are accessed by iterating over the list.</p> <p>A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">empty</span> <span class="o">=</span> <span class="p">()</span> <span class="gp">>>> </span><span class="n">singleton</span> <span class="o">=</span> <span class="s">'hello'</span><span class="p">,</span> <span class="c"># <-- note trailing comma</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">empty</span><span class="p">)</span> <span class="go">0</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">singleton</span><span class="p">)</span> <span class="go">1</span> <span class="gp">>>> </span><span class="n">singleton</span> <span class="go">('hello',)</span> </pre></div> </div> <p>The statement <tt class="docutils literal"><span class="pre">t</span> <span class="pre">=</span> <span class="pre">12345,</span> <span class="pre">54321,</span> <span class="pre">'hello!'</span></tt> is an example of <em>tuple packing</em>: the values <tt class="docutils literal"><span class="pre">12345</span></tt>, <tt class="docutils literal"><span class="pre">54321</span></tt> and <tt class="docutils literal"><span class="pre">'hello!'</span></tt> are packed together in a tuple. The reverse operation is also possible:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> </pre></div> </div> <p>This is called, appropriately enough, <em>sequence unpacking</em> and works for any sequence on the right-hand side. Sequence unpacking requires the list of variables on the left to have the same number of elements as the length of the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.</p> </div> <div class="section" id="sets"> <span id="tut-sets"></span><h2>5.4. Sets<a class="headerlink" href="#sets" title="Permalink to this headline">¶</a></h2> <p>Python also includes a data type for <em>sets</em>. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.</p> <p>Curly braces or the <a class="reference internal" href="../library/stdtypes.html#set" title="set"><tt class="xref py py-func docutils literal"><span class="pre">set()</span></tt></a> function can be used to create sets. Note: to create an empty set you have to use <tt class="docutils literal"><span class="pre">set()</span></tt>, not <tt class="docutils literal"><span class="pre">{}</span></tt>; the latter creates an empty dictionary, a data structure that we discuss in the next section.</p> <p>Here is a brief demonstration:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">basket</span> <span class="o">=</span> <span class="p">[</span><span class="s">'apple'</span><span class="p">,</span> <span class="s">'orange'</span><span class="p">,</span> <span class="s">'apple'</span><span class="p">,</span> <span class="s">'pear'</span><span class="p">,</span> <span class="s">'orange'</span><span class="p">,</span> <span class="s">'banana'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">fruit</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">basket</span><span class="p">)</span> <span class="c"># create a set without duplicates</span> <span class="gp">>>> </span><span class="n">fruit</span> <span class="go">set(['orange', 'pear', 'apple', 'banana'])</span> <span class="gp">>>> </span><span class="s">'orange'</span> <span class="ow">in</span> <span class="n">fruit</span> <span class="c"># fast membership testing</span> <span class="go">True</span> <span class="gp">>>> </span><span class="s">'crabgrass'</span> <span class="ow">in</span> <span class="n">fruit</span> <span class="go">False</span> <span class="gp">>>> </span><span class="c"># Demonstrate set operations on unique letters from two words</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s">'abracadabra'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s">'alacazam'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">a</span> <span class="c"># unique letters in a</span> <span class="go">set(['a', 'r', 'b', 'c', 'd'])</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="c"># letters in a but not in b</span> <span class="go">set(['r', 'd', 'b'])</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">|</span> <span class="n">b</span> <span class="c"># letters in either a or b</span> <span class="go">set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">&</span> <span class="n">b</span> <span class="c"># letters in both a and b</span> <span class="go">set(['a', 'c'])</span> <span class="gp">>>> </span><span class="n">a</span> <span class="o">^</span> <span class="n">b</span> <span class="c"># letters in a or b but not both</span> <span class="go">set(['r', 'd', 'b', 'm', 'z', 'l'])</span> </pre></div> </div> <p>Similarly to <a class="reference internal" href="#tut-listcomps"><em>list comprehensions</em></a>, set comprehensions are also supported:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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="s">'abracadabra'</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s">'abc'</span><span class="p">}</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">set(['r', 'd'])</span> </pre></div> </div> </div> <div class="section" id="dictionaries"> <span id="tut-dictionaries"></span><h2>5.5. Dictionaries<a class="headerlink" href="#dictionaries" title="Permalink to this headline">¶</a></h2> <p>Another useful data type built into Python is the <em>dictionary</em> (see <a class="reference internal" href="../library/stdtypes.html#typesmapping"><em>Mapping Types — dict</em></a>). Dictionaries are sometimes found in other languages as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by <em>keys</em>, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can’t use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like <tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">extend()</span></tt>.</p> <p>It is best to think of a dictionary as an unordered set of <em>key: value</em> pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: <tt class="docutils literal"><span class="pre">{}</span></tt>. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.</p> <p>The main operations on a dictionary are storing a value with some key and extracting the value given the key. It is also possible to delete a key:value pair with <tt class="docutils literal"><span class="pre">del</span></tt>. If you store using a key that is already in use, the old value associated with that key is forgotten. It is an error to extract a value using a non-existent key.</p> <p>The <tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt> method of a dictionary object returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just apply the <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a> function to it). To check whether a single key is in the dictionary, use the <a class="reference internal" href="../reference/expressions.html#in"><tt class="xref std std-keyword docutils literal"><span class="pre">in</span></tt></a> keyword.</p> <p>Here is a small example using a dictionary:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tel</span> <span class="o">=</span> <span class="p">{</span><span class="s">'jack'</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s">'sape'</span><span class="p">:</span> <span class="mi">4139</span><span class="p">}</span> <span class="gp">>>> </span><span class="n">tel</span><span class="p">[</span><span class="s">'guido'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4127</span> <span class="gp">>>> </span><span class="n">tel</span> <span class="go">{'sape': 4139, 'guido': 4127, 'jack': 4098}</span> <span class="gp">>>> </span><span class="n">tel</span><span class="p">[</span><span class="s">'jack'</span><span class="p">]</span> <span class="go">4098</span> <span class="gp">>>> </span><span class="k">del</span> <span class="n">tel</span><span class="p">[</span><span class="s">'sape'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">tel</span><span class="p">[</span><span class="s">'irv'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4127</span> <span class="gp">>>> </span><span class="n">tel</span> <span class="go">{'guido': 4127, 'irv': 4127, 'jack': 4098}</span> <span class="gp">>>> </span><span class="n">tel</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="go">['guido', 'irv', 'jack']</span> <span class="gp">>>> </span><span class="s">'guido'</span> <span class="ow">in</span> <span class="n">tel</span> <span class="go">True</span> </pre></div> </div> <p>The <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-func docutils literal"><span class="pre">dict()</span></tt></a> constructor builds dictionaries directly from sequences of key-value pairs:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">dict</span><span class="p">([(</span><span class="s">'sape'</span><span class="p">,</span> <span class="mi">4139</span><span class="p">),</span> <span class="p">(</span><span class="s">'guido'</span><span class="p">,</span> <span class="mi">4127</span><span class="p">),</span> <span class="p">(</span><span class="s">'jack'</span><span class="p">,</span> <span class="mi">4098</span><span class="p">)])</span> <span class="go">{'sape': 4139, 'jack': 4098, 'guido': 4127}</span> </pre></div> </div> <p>In addition, dict comprehensions can be used to create dictionaries from arbitrary key and value expressions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="go">{2: 4, 4: 16, 6: 36}</span> </pre></div> </div> <p>When the keys are simple strings, it is sometimes easier to specify pairs using keyword arguments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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> <span class="go">{'sape': 4139, 'jack': 4098, 'guido': 4127}</span> </pre></div> </div> </div> <div class="section" id="looping-techniques"> <span id="tut-loopidioms"></span><h2>5.6. Looping Techniques<a class="headerlink" href="#looping-techniques" title="Permalink to this headline">¶</a></h2> <p>When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the <a class="reference internal" href="../library/functions.html#enumerate" title="enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a> function.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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="s">'tic'</span><span class="p">,</span> <span class="s">'tac'</span><span class="p">,</span> <span class="s">'toe'</span><span class="p">]):</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="gp">...</span> <span class="go">0 tic</span> <span class="go">1 tac</span> <span class="go">2 toe</span> </pre></div> </div> <p>To loop over two or more sequences at the same time, the entries can be paired with the <a class="reference internal" href="../library/functions.html#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a> function.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">questions</span> <span class="o">=</span> <span class="p">[</span><span class="s">'name'</span><span class="p">,</span> <span class="s">'quest'</span><span class="p">,</span> <span class="s">'favorite color'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">answers</span> <span class="o">=</span> <span class="p">[</span><span class="s">'lancelot'</span><span class="p">,</span> <span class="s">'the holy grail'</span><span class="p">,</span> <span class="s">'blue'</span><span class="p">]</span> <span class="gp">>>> </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> <span class="gp">... </span> <span class="k">print</span> <span class="s">'What is your {0}? It is {1}.'</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> <span class="gp">...</span> <span class="go">What is your name? It is lancelot.</span> <span class="go">What is your quest? It is the holy grail.</span> <span class="go">What is your favorite color? It is blue.</span> </pre></div> </div> <p>To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><tt class="xref py py-func docutils literal"><span class="pre">reversed()</span></tt></a> function.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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">xrange</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> <span class="gp">... </span> <span class="k">print</span> <span class="n">i</span> <span class="gp">...</span> <span class="go">9</span> <span class="go">7</span> <span class="go">5</span> <span class="go">3</span> <span class="go">1</span> </pre></div> </div> <p>To loop over a sequence in sorted order, use the <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a> function which returns a new sorted list while leaving the source unaltered.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">basket</span> <span class="o">=</span> <span class="p">[</span><span class="s">'apple'</span><span class="p">,</span> <span class="s">'orange'</span><span class="p">,</span> <span class="s">'apple'</span><span class="p">,</span> <span class="s">'pear'</span><span class="p">,</span> <span class="s">'orange'</span><span class="p">,</span> <span class="s">'banana'</span><span class="p">]</span> <span class="gp">>>> </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> <span class="gp">... </span> <span class="k">print</span> <span class="n">f</span> <span class="gp">...</span> <span class="go">apple</span> <span class="go">banana</span> <span class="go">orange</span> <span class="go">pear</span> </pre></div> </div> <p>When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the <tt class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></tt> method.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">knights</span> <span class="o">=</span> <span class="p">{</span><span class="s">'gallahad'</span><span class="p">:</span> <span class="s">'the pure'</span><span class="p">,</span> <span class="s">'robin'</span><span class="p">:</span> <span class="s">'the brave'</span><span class="p">}</span> <span class="gp">>>> </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">iteritems</span><span class="p">():</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="gp">...</span> <span class="go">gallahad the pure</span> <span class="go">robin the brave</span> </pre></div> </div> <p>To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy. Looping over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="s">'cat'</span><span class="p">,</span> <span class="s">'window'</span><span class="p">,</span> <span class="s">'defenestrate'</span><span class="p">]</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">w</span> <span class="ow">in</span> <span class="n">words</span><span class="p">[:]:</span> <span class="c"># Loop over a slice copy of the entire list.</span> <span class="gp">... </span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">w</span><span class="p">)</span> <span class="o">></span> <span class="mi">6</span><span class="p">:</span> <span class="gp">... </span> <span class="n">words</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">words</span> <span class="go">['defenestrate', 'cat', 'window', 'defenestrate']</span> </pre></div> </div> </div> <div class="section" id="more-on-conditions"> <span id="tut-conditions"></span><h2>5.7. More on Conditions<a class="headerlink" href="#more-on-conditions" title="Permalink to this headline">¶</a></h2> <p>The conditions used in <tt class="docutils literal"><span class="pre">while</span></tt> and <tt class="docutils literal"><span class="pre">if</span></tt> statements can contain any operators, not just comparisons.</p> <p>The comparison operators <tt class="docutils literal"><span class="pre">in</span></tt> and <tt class="docutils literal"><span class="pre">not</span> <span class="pre">in</span></tt> check whether a value occurs (does not occur) in a sequence. The operators <tt class="docutils literal"><span class="pre">is</span></tt> and <tt class="docutils literal"><span class="pre">is</span> <span class="pre">not</span></tt> compare whether two objects are really the same object; this only matters for mutable objects like lists. All comparison operators have the same priority, which is lower than that of all numerical operators.</p> <p>Comparisons can be chained. For example, <tt class="docutils literal"><span class="pre">a</span> <span class="pre"><</span> <span class="pre">b</span> <span class="pre">==</span> <span class="pre">c</span></tt> tests whether <tt class="docutils literal"><span class="pre">a</span></tt> is less than <tt class="docutils literal"><span class="pre">b</span></tt> and moreover <tt class="docutils literal"><span class="pre">b</span></tt> equals <tt class="docutils literal"><span class="pre">c</span></tt>.</p> <p>Comparisons may be combined using the Boolean operators <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt>, and the outcome of a comparison (or of any other Boolean expression) may be negated with <tt class="docutils literal"><span class="pre">not</span></tt>. These have lower priorities than comparison operators; between them, <tt class="docutils literal"><span class="pre">not</span></tt> has the highest priority and <tt class="docutils literal"><span class="pre">or</span></tt> the lowest, so that <tt class="docutils literal"><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></tt> is equivalent to <tt class="docutils literal"><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></tt>. As always, parentheses can be used to express the desired composition.</p> <p>The Boolean operators <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt> are so-called <em>short-circuit</em> operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if <tt class="docutils literal"><span class="pre">A</span></tt> and <tt class="docutils literal"><span class="pre">C</span></tt> are true but <tt class="docutils literal"><span class="pre">B</span></tt> is false, <tt class="docutils literal"><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></tt> does not evaluate the expression <tt class="docutils literal"><span class="pre">C</span></tt>. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.</p> <p>It is possible to assign the result of a comparison or other Boolean expression to a variable. For example,</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </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="s">''</span><span class="p">,</span> <span class="s">'Trondheim'</span><span class="p">,</span> <span class="s">'Hammer Dance'</span> <span class="gp">>>> </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> <span class="gp">>>> </span><span class="n">non_null</span> <span class="go">'Trondheim'</span> </pre></div> </div> <p>Note that in Python, unlike C, assignment cannot occur inside expressions. C programmers may grumble about this, but it avoids a common class of problems encountered in C programs: typing <tt class="docutils literal"><span class="pre">=</span></tt> in an expression when <tt class="docutils literal"><span class="pre">==</span></tt> was intended.</p> </div> <div class="section" id="comparing-sequences-and-other-types"> <span id="tut-comparing"></span><h2>5.8. Comparing Sequences and Other Types<a class="headerlink" href="#comparing-sequences-and-other-types" title="Permalink to this headline">¶</a></h2> <p>Sequence objects may be compared to other objects with the same sequence type. The comparison uses <em>lexicographical</em> ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one. Lexicographical ordering for strings uses the ASCII ordering for individual characters. Some examples of comparisons between sequences of the same type:</p> <div class="highlight-python"><div class="highlight"><pre><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="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> <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="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> <span class="s">'ABC'</span> <span class="o"><</span> <span class="s">'C'</span> <span class="o"><</span> <span class="s">'Pascal'</span> <span class="o"><</span> <span class="s">'Python'</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="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">4</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="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="o">-</span><span class="mi">1</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="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> <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="s">'aa'</span><span class="p">,</span> <span class="s">'ab'</span><span class="p">))</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="p">(</span><span class="s">'abc'</span><span class="p">,</span> <span class="s">'a'</span><span class="p">),</span> <span class="mi">4</span><span class="p">)</span> </pre></div> </div> <p>Note that comparing objects of different types is legal. The outcome is deterministic but arbitrary: the types are ordered by their name. Thus, a list is always smaller than a string, a string is always smaller than a tuple, etc. <a class="footnote-reference" href="#id2" id="id1">[1]</a> Mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc.</p> <p class="rubric">Footnotes</p> <table class="docutils footnote" frame="void" id="id2" rules="none"> <colgroup><col class="label" /><col /></colgroup> <tbody valign="top"> <tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>The rules for comparing objects of different types should not be relied upon; they may change in a future version of the language.</td></tr> </tbody> </table> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">5. Data Structures</a><ul> <li><a class="reference internal" href="#more-on-lists">5.1. More on Lists</a><ul> <li><a class="reference internal" href="#using-lists-as-stacks">5.1.1. Using Lists as Stacks</a></li> <li><a class="reference internal" href="#using-lists-as-queues">5.1.2. Using Lists as Queues</a></li> <li><a class="reference internal" href="#functional-programming-tools">5.1.3. Functional Programming Tools</a></li> <li><a class="reference internal" href="#list-comprehensions">5.1.4. List Comprehensions</a><ul> <li><a class="reference internal" href="#nested-list-comprehensions">5.1.4.1. Nested List Comprehensions</a></li> </ul> </li> </ul> </li> <li><a class="reference internal" href="#the-del-statement">5.2. The <tt class="docutils literal"><span class="pre">del</span></tt> statement</a></li> <li><a class="reference internal" href="#tuples-and-sequences">5.3. Tuples and Sequences</a></li> <li><a class="reference internal" href="#sets">5.4. Sets</a></li> <li><a class="reference internal" href="#dictionaries">5.5. Dictionaries</a></li> <li><a class="reference internal" href="#looping-techniques">5.6. Looping Techniques</a></li> <li><a class="reference internal" href="#more-on-conditions">5.7. More on Conditions</a></li> <li><a class="reference internal" href="#comparing-sequences-and-other-types">5.8. Comparing Sequences and Other Types</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="controlflow.html" title="previous chapter">4. More Control Flow Tools</a></p> <h4>Next topic</h4> <p class="topless"><a href="modules.html" title="next chapter">6. Modules</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/tutorial/datastructures.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="modules.html" title="6. Modules" >next</a> |</li> <li class="right" > <a href="controlflow.html" title="4. More Control Flow Tools" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Tutorial</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close