SEESenv/web/html/backup/ch02-list_tuples.html
changeset 2 52d12eb31c30
parent 0 8083d21c0020
equal deleted inserted replaced
1:672eaaab9204 2:52d12eb31c30
       
     1 <html>
       
     2 <head>
       
     3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
       
     4 <title>Lists and Tuples</title>
       
     5 <link rel="stylesheet" href="hgbook.css" type="text/css">
       
     6 <meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
       
     7 <link rel="home" href="index.html" title="Chapter 1. List and Tuples">
       
     8 <link rel="up" href="index.html" title="Chapter 1. List and Tuples">
       
     9 <link rel="prev" href="index.html" title="Chapter 1. List and Tuples">
       
    10 </head>
       
    11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
       
    12 <div class="navheader">
       
    13 <table width="100%" summary="Navigation header">
       
    14 <tr><th colspan="3" align="center">Lists and Tuples</th></tr>
       
    15 <tr>
       
    16 <td width="20%" align="left">
       
    17 <a accesskey="p" href="index.html">Prev</a> </td>
       
    18 <th width="60%" align="center"> </th>
       
    19 <td width="20%" align="right"> </td>
       
    20 </tr>
       
    21 </table>
       
    22 <hr>
       
    23 </div>
       
    24 <div class="article" title="Lists and Tuples">
       
    25 <div class="titlepage">
       
    26 <div><div><h2 class="title">
       
    27 <a name="id2609714"></a>Lists and Tuples</h2></div></div>
       
    28 <hr>
       
    29 </div>
       
    30 <div class="toc">
       
    31 <p><b>Table of Contents</b></p>
       
    32 <dl>
       
    33 <dt><span class="section"><a href="ar01.html#common-list-operations">1. Common List Operations</a></span></dt>
       
    34 <dd><dl>
       
    35 <dt><span class="section"><a href="ar01.html#indexing">1.1. Indexing</a></span></dt>
       
    36 <dt><span class="section"><a href="ar01.html#concatenating">1.2. Concatenating</a></span></dt>
       
    37 <dt><span class="section"><a href="ar01.html#slicing">1.3. Slicing</a></span></dt>
       
    38 <dt><span class="section"><a href="ar01.html#multiplication">1.4. Multiplication</a></span></dt>
       
    39 <dt><span class="section"><a href="ar01.html#membership">1.5. Membership</a></span></dt>
       
    40 <dt><span class="section"><a href="ar01.html#length-maximum-and-minimum">1.6. Length, Maximum and Minimum</a></span></dt>
       
    41 <dt><span class="section"><a href="ar01.html#changing-elements">1.7. Changing Elements</a></span></dt>
       
    42 <dt><span class="section"><a href="ar01.html#deleting-elements">1.8. Deleting Elements</a></span></dt>
       
    43 <dt><span class="section"><a href="ar01.html#assign-to-slices">1.9. Assign to Slices</a></span></dt>
       
    44 </dl></dd>
       
    45 <dt><span class="section"><a href="ar01.html#none-empty-lists-and-initialization">2. None, Empty Lists, and Initialization</a></span></dt>
       
    46 <dt><span class="section"><a href="ar01.html#nested-lists">3. Nested Lists</a></span></dt>
       
    47 <dt><span class="section"><a href="ar01.html#list-methods">4. List Methods</a></span></dt>
       
    48 <dd><dl>
       
    49 <dt><span class="section"><a href="ar01.html#append">4.1. append</a></span></dt>
       
    50 <dt><span class="section"><a href="ar01.html#count">4.2. count</a></span></dt>
       
    51 <dt><span class="section"><a href="ar01.html#extend">4.3. extend</a></span></dt>
       
    52 <dt><span class="section"><a href="ar01.html#index">4.4. index</a></span></dt>
       
    53 <dt><span class="section"><a href="ar01.html#insert">4.5. insert</a></span></dt>
       
    54 <dt><span class="section"><a href="ar01.html#pop">4.6. pop</a></span></dt>
       
    55 <dt><span class="section"><a href="ar01.html#remove">4.7. remove</a></span></dt>
       
    56 <dt><span class="section"><a href="ar01.html#reverse">4.8. reverse</a></span></dt>
       
    57 <dt><span class="section"><a href="ar01.html#sort">4.9. sort</a></span></dt>
       
    58 </dl></dd>
       
    59 <dt><span class="section"><a href="ar01.html#tuples">5. Tuples</a></span></dt>
       
    60 <dd><dl><dt><span class="section"><a href="ar01.html#common-tuple-operations">5.1. Common Tuple Operations</a></span></dt></dl></dd>
       
    61 <dt><span class="section"><a href="ar01.html#additional-syntax">6. Additional Syntax</a></span></dt>
       
    62 <dd><dl>
       
    63 <dt><span class="section"><a href="ar01.html#range">6.1. range()</a></span></dt>
       
    64 <dt><span class="section"><a href="ar01.html#for">6.2. for</a></span></dt>
       
    65 </dl></dd>
       
    66 <dt><span class="section"><a href="ar01.html#conclusion">7. Conclusion</a></span></dt>
       
    67 </dl>
       
    68 </div>Lists
       
    69   <p><a name="x_1"></a>Python provides an intuitive way to represent a group items, called 
       
    70     <span class="emphasis"><em>Lists</em></span>. The items of a 
       
    71     <span class="emphasis"><em>List</em></span> are called its elements. Unlike C/C++, elements can be of any type. A 
       
    72     <span class="emphasis"><em>List</em></span> is represented as a list of comma-sepated elements with square brackets around them:
       
    73   </p>
       
    74 <pre class="programlisting">&gt;&gt;&gt; a = [10, 'Python programming', 20.3523, 23, 3534534L]
       
    75 &gt;&gt;&gt; a
       
    76 [10, 'Python programming', 20.3523, 23, 3534534L]
       
    77 
       
    78 
       
    79 </pre>
       
    80 <div class="section" title="1. Common List Operations">
       
    81 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
    82 <a name="common-list-operations"></a>1. Common List Operations</h2></div></div></div>
       
    83 <p><a name="x_2"></a>The following are some of the most commonly used operations on 
       
    84       <span class="emphasis"><em>Lists</em></span>.
       
    85     </p>
       
    86 <div class="section" title="1.1. Indexing">
       
    87 <div class="titlepage"><div><div><h3 class="title">
       
    88 <a name="indexing"></a>1.1. Indexing</h3></div></div></div>
       
    89 <p><a name="x_3"></a>Individual elements of a 
       
    90         <span class="emphasis"><em>List</em></span> can be accessed using an index to the element. The indices start at 0. One can also access the elements of the 
       
    91         <span class="emphasis"><em>List</em></span> in reverse using negative indices.:
       
    92       </p>
       
    93 <pre class="programlisting">&gt;&gt;&gt; a[1]
       
    94 'Python programming'
       
    95 &gt;&gt;&gt; a[-1]
       
    96 3534534L
       
    97 
       
    98 </pre>
       
    99 <p><a name="x_4"></a>It is important to note here that the last element of the 
       
   100         <span class="emphasis"><em>List</em></span> has an index of -1.
       
   101       </p>
       
   102 </div>
       
   103 <div class="section" title="1.2. Concatenating">
       
   104 <div class="titlepage"><div><div><h3 class="title">
       
   105 <a name="concatenating"></a>1.2. Concatenating</h3></div></div></div>
       
   106 <p><a name="x_5"></a>Two or more 
       
   107         <span class="emphasis"><em>Lists</em></span> can be concatenated using the + operator:
       
   108       </p>
       
   109 <pre class="programlisting">&gt;&gt;&gt; a + ['foo', 12, 23.3432, 54]
       
   110 [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54]
       
   111 &gt;&gt;&gt; [54, 75, 23] + ['write', 67, 'read']
       
   112 [54, 75, 23, 'write', 67, 'read']
       
   113 
       
   114 
       
   115 </pre>
       
   116 </div>
       
   117 <div class="section" title="1.3. Slicing">
       
   118 <div class="titlepage"><div><div><h3 class="title">
       
   119 <a name="slicing"></a>1.3. Slicing</h3></div></div></div>
       
   120 <p><a name="x_6"></a>A 
       
   121         <span class="emphasis"><em>List</em></span> can be sliced off to contain a subset of elements of the 
       
   122         <span class="emphasis"><em>List</em></span>. Slicing can be done by using two indices separated by a colon, where the first index is inclusive and the second index is exclusive. The resulting slice is also a 
       
   123         <span class="emphasis"><em>List</em></span>.:
       
   124       </p>
       
   125 <pre class="programlisting">&gt;&gt;&gt; num = [1, 2, 3, 4, 5, 6, 7, 8, 9]
       
   126 &gt;&gt;&gt; num[3:6]
       
   127 [4, 5, 6]
       
   128 &gt;&gt;&gt; num[0:1]
       
   129 [1]
       
   130 &gt;&gt;&gt; num[7:10]
       
   131 [7, 8, 9]
       
   132 
       
   133 </pre>
       
   134 <p><a name="x_7"></a>The last example showed how to access last 3 elements of the 
       
   135         <span class="emphasis"><em>List</em></span>. There is a small catch here. The second index 10 actually refers to the 11th element of the 
       
   136         <span class="emphasis"><em>List</em></span> which is still valid, even though it doesn't exist because the second index is exclusive and tells the Python interpreter to get the last element of the 
       
   137         <span class="emphasis"><em>List</em></span>. But this can also be done in a much easier way using negative indices:
       
   138       </p>
       
   139 <pre class="programlisting">&gt;&gt;&gt; num[-3:-1]
       
   140 [7, 8, 9]
       
   141 
       
   142 </pre>
       
   143 <p><a name="x_8"></a>Excluding the first index implies that the slice must start at the beginning of the 
       
   144         <span class="emphasis"><em>List</em></span>, while excluding the second index includes all the elements till the end of the 
       
   145         <span class="emphasis"><em>List</em></span>. A third parameter to a slice, which is implicitly taken as 1 is the step of the slice. It is specified as a value which follows a colon after the second index:
       
   146       </p>
       
   147 <pre class="programlisting">&gt;&gt;&gt; num[:4]
       
   148 [1, 2, 3, 4]
       
   149 &gt;&gt;&gt; num[7:]
       
   150 [8, 9]
       
   151 &gt;&gt;&gt; num[-3:]
       
   152 [7, 8, 9]
       
   153 &gt;&gt;&gt; num[:]
       
   154 [1, 2, 3, 4, 5, 6, 7, 8, 9]
       
   155 &gt;&gt;&gt; num[4:9:3]
       
   156 [5, 8]
       
   157 &gt;&gt;&gt; num[3::2]
       
   158 [4, 6, 8]
       
   159 &gt;&gt;&gt; num[::4]
       
   160 [1, 5, 9]
       
   161 
       
   162 
       
   163 </pre>
       
   164 </div>
       
   165 <div class="section" title="1.4. Multiplication">
       
   166 <div class="titlepage"><div><div><h3 class="title">
       
   167 <a name="multiplication"></a>1.4. Multiplication</h3></div></div></div>
       
   168 <p><a name="x_9"></a>A 
       
   169         <span class="emphasis"><em>List</em></span> can be multiplied with an integer to repeat itself:
       
   170       </p>
       
   171 <pre class="programlisting">&gt;&gt;&gt; [20] * 5
       
   172 [20, 20, 20, 20, 20]
       
   173 &gt;&gt;&gt; [42, 'Python', 54] * 3
       
   174 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54]
       
   175 
       
   176 
       
   177 </pre>
       
   178 </div>
       
   179 <div class="section" title="1.5. Membership">
       
   180 <div class="titlepage"><div><div><h3 class="title">
       
   181 <a name="membership"></a>1.5. Membership</h3></div></div></div>
       
   182 <p><a name="x_a"></a>
       
   183         <span class="strong"><strong>in</strong></span> operator is used to find whether an element is part of the 
       
   184         <span class="emphasis"><em>List</em></span>. It returns 
       
   185         <span class="strong"><strong>True</strong></span> if the element is present in the 
       
   186         <span class="emphasis"><em>List</em></span> or 
       
   187         <span class="strong"><strong>False</strong></span> if it is not present. Since this operator returns a Boolean value it is called a Boolean operator:
       
   188       </p>
       
   189 <pre class="programlisting">&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
       
   190 &gt;&gt;&gt; 'Tim' in names
       
   191 True
       
   192 &gt;&gt;&gt; 'Adam' in names
       
   193 False
       
   194 
       
   195 
       
   196 </pre>
       
   197 </div>
       
   198 <div class="section" title="1.6. Length, Maximum and Minimum">
       
   199 <div class="titlepage"><div><div><h3 class="title">
       
   200 <a name="length-maximum-and-minimum"></a>1.6. Length, Maximum and Minimum</h3></div></div></div>
       
   201 <p><a name="x_b"></a>Length of a 
       
   202         <span class="emphasis"><em>List</em></span> can be found out using the len function. The max function returns the element with the largest value and the min function returns the element with the smallest value:
       
   203       </p>
       
   204 <pre class="programlisting">&gt;&gt;&gt; num = [4, 1, 32, 12, 67, 34, 65]
       
   205 &gt;&gt;&gt; len(num)
       
   206 7
       
   207 &gt;&gt;&gt; max(num)
       
   208 67
       
   209 &gt;&gt;&gt; min(num)
       
   210 1
       
   211 
       
   212 
       
   213 </pre>
       
   214 </div>
       
   215 <div class="section" title="1.7. Changing Elements">
       
   216 <div class="titlepage"><div><div><h3 class="title">
       
   217 <a name="changing-elements"></a>1.7. Changing Elements</h3></div></div></div>
       
   218 <p><a name="x_c"></a>Unlike Strings 
       
   219         <span class="emphasis"><em>Lists</em></span> are mutable, i.e. elements of a 
       
   220         <span class="emphasis"><em>List</em></span> can be manipulated:
       
   221       </p>
       
   222 <pre class="programlisting">&gt;&gt;&gt; a = [1, 3, 5, 7]
       
   223 &gt;&gt;&gt; a[2] = 9
       
   224 &gt;&gt;&gt; a
       
   225 [1, 3, 9, 7]
       
   226 
       
   227 
       
   228 </pre>
       
   229 </div>
       
   230 <div class="section" title="1.8. Deleting Elements">
       
   231 <div class="titlepage"><div><div><h3 class="title">
       
   232 <a name="deleting-elements"></a>1.8. Deleting Elements</h3></div></div></div>
       
   233 <p><a name="x_d"></a>An element or a slice of a 
       
   234         <span class="emphasis"><em>List</em></span> can be deleted by using the 
       
   235         <span class="strong"><strong>del</strong></span> statement:
       
   236       </p>
       
   237 <pre class="programlisting">&gt;&gt;&gt; a = [1, 3, 5, 7, 9, 11]
       
   238 &gt;&gt;&gt; del a[-2:]
       
   239 &gt;&gt;&gt; a
       
   240 [1, 3, 5, 7]
       
   241 &gt;&gt;&gt; del a[1]
       
   242 &gt;&gt;&gt; a
       
   243 [1, 5, 7]
       
   244 
       
   245 
       
   246 </pre>
       
   247 </div>
       
   248 <div class="section" title="1.9. Assign to Slices">
       
   249 <div class="titlepage"><div><div><h3 class="title">
       
   250 <a name="assign-to-slices"></a>1.9. Assign to Slices</h3></div></div></div>
       
   251 <p><a name="x_e"></a>In the same way, values can be assigned to individual elements of the 
       
   252         <span class="emphasis"><em>List</em></span>, a 
       
   253         <span class="emphasis"><em>List</em></span> of elements can be assigned to a slice:
       
   254       </p>
       
   255 <pre class="programlisting">&gt;&gt;&gt; a = [2, 3, 4, 5]
       
   256 &gt;&gt;&gt; a[:2] = [0, 1]
       
   257 [0, 1, 4, 5]
       
   258 &gt;&gt;&gt; a[2:2] = [2, 3]
       
   259 &gt;&gt;&gt; a
       
   260 [0, 1, 2, 3, 4, 5]
       
   261 &gt;&gt;&gt; a[2:4] = []
       
   262 &gt;&gt;&gt; a
       
   263 [0, 1, 4, 5]
       
   264 
       
   265 </pre>
       
   266 <p><a name="x_f"></a>The last two examples should be particularly noted carefully. The last but one example insert elements or a list of elements into a 
       
   267         <span class="emphasis"><em>List</em></span> and the last example deletes a list of elements from the 
       
   268         <span class="emphasis"><em>List</em></span>.
       
   269       </p>
       
   270 </div>
       
   271 </div>
       
   272 <div class="section" title="2. None, Empty Lists, and Initialization">
       
   273 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   274 <a name="none-empty-lists-and-initialization"></a>2. None, Empty Lists, and Initialization</h2></div></div></div>
       
   275 <p><a name="x_10"></a>An 
       
   276       <span class="emphasis"><em>Empty List</em></span> is a 
       
   277       <span class="emphasis"><em>List</em></span> with no elements and is simply represented as []. A 
       
   278       <span class="emphasis"><em>None List</em></span> is one with all elements in it being 
       
   279       <span class="strong"><strong>None</strong></span>. It serves the purpose having a container list of some fixed number of elements with no value:
       
   280     </p>
       
   281 <pre class="programlisting">&gt;&gt;&gt; a = []
       
   282 &gt;&gt;&gt; a
       
   283 []
       
   284 &gt;&gt;&gt; n = [None] * 10
       
   285 &gt;&gt;&gt; n
       
   286 [None, None, None, None, None, None, None, None, None, None]
       
   287 
       
   288 
       
   289 </pre>
       
   290 </div>
       
   291 <div class="section" title="3. Nested Lists">
       
   292 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   293 <a name="nested-lists"></a>3. Nested Lists</h2></div></div></div>
       
   294 <p><a name="x_11"></a>As mentioned earlier, a List can contain elements of any data type. This also implies a 
       
   295       <span class="emphasis"><em>List</em></span> can have a 
       
   296       <span class="emphasis"><em>Lists</em></span> themselves as its elements. These are called as 
       
   297       <span class="emphasis"><em>Nested Lists</em></span>. There is no limit on the depth of the 
       
   298       <span class="emphasis"><em>Nested Lists</em></span>:
       
   299     </p>
       
   300 <pre class="programlisting">&gt;&gt;&gt; a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7]
       
   301 
       
   302 
       
   303 </pre>
       
   304 </div>
       
   305 <div class="section" title="4. List Methods">
       
   306 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   307 <a name="list-methods"></a>4. List Methods</h2></div></div></div>
       
   308 <p><a name="x_12"></a>A method is a function that is coupled to an object. More about objects and its methods are discussed in Advanced Python module. In general, a method is called like:</p>
       
   309 <pre class="programlisting">object.method(arguments)
       
   310 
       
   311 </pre>
       
   312 <p><a name="x_13"></a>For now, it is enough to know that a list of elements is an object and so 
       
   313       <span class="emphasis"><em>List</em></span> methods can be called upon them. Also some of the methods change the 
       
   314       <span class="emphasis"><em>List</em></span> in-place, meaning it modifies the existing list instead of creating a new one, while other methods don't. It must be noted as we run through the 
       
   315       <span class="emphasis"><em>List</em></span> methods.
       
   316     </p>
       
   317 <p><a name="x_14"></a>Some of the most commonly used 
       
   318       <span class="emphasis"><em>List</em></span> methods are as follows:
       
   319     </p>
       
   320 <div class="section" title="4.1. append">
       
   321 <div class="titlepage"><div><div><h3 class="title">
       
   322 <a name="append"></a>4.1. append</h3></div></div></div>
       
   323 <p><a name="x_15"></a>The 
       
   324         <span class="emphasis"><em>append</em></span> method is used to append an object at the end of the list:
       
   325       </p>
       
   326 <pre class="programlisting">&gt;&gt;&gt; prime = [2, 3, 5]
       
   327 &gt;&gt;&gt; prime.append(7)
       
   328 &gt;&gt;&gt; prime
       
   329 [2, 3, 5, 7]
       
   330 
       
   331 </pre>
       
   332 <p><a name="x_16"></a>It is important to note that append changes the 
       
   333         <span class="emphasis"><em>List</em></span> in-place.
       
   334       </p>
       
   335 </div>
       
   336 <div class="section" title="4.2. count">
       
   337 <div class="titlepage"><div><div><h3 class="title">
       
   338 <a name="count"></a>4.2. count</h3></div></div></div>
       
   339 <p><a name="x_17"></a>The 
       
   340         <span class="emphasis"><em>count</em></span> method returns the number of occurences of a particular element in a list:
       
   341       </p>
       
   342 <pre class="programlisting">&gt;&gt;&gt; [1, 4, 4, 9, 9, 9].count(9)
       
   343 3
       
   344 &gt;&gt;&gt; tlst = ['Python', 'is', 'a', 'beautiful', 'language']
       
   345 &gt;&gt;&gt; tlst.count('Python')
       
   346 1
       
   347 
       
   348 
       
   349 </pre>
       
   350 </div>
       
   351 <div class="section" title="4.3. extend">
       
   352 <div class="titlepage"><div><div><h3 class="title">
       
   353 <a name="extend"></a>4.3. extend</h3></div></div></div>
       
   354 <p><a name="x_18"></a>The 
       
   355         <span class="emphasis"><em>extend</em></span> method extends the list on which it is called by the list supplied as argument to it:
       
   356       </p>
       
   357 <pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3]
       
   358 &gt;&gt;&gt; b = [4, 5, 6]
       
   359 &gt;&gt;&gt; a.extend(b)
       
   360 [1, 2, 3, 4, 5, 6]
       
   361 
       
   362 </pre>
       
   363 <p><a name="x_19"></a>This is an in-place method. This method is equivalent to using the + operator, but using the + operator returns a new list.</p>
       
   364 </div>
       
   365 <div class="section" title="4.4. index">
       
   366 <div class="titlepage"><div><div><h3 class="title">
       
   367 <a name="index"></a>4.4. index</h3></div></div></div>
       
   368 <p><a name="x_1a"></a>The 
       
   369         <span class="emphasis"><em>index</em></span> method returns the index position of the element in the list specified as argument:
       
   370       </p>
       
   371 <pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, ,4, 5]
       
   372 &gt;&gt;&gt; a.index(4)
       
   373 3
       
   374 
       
   375 
       
   376 </pre>
       
   377 </div>
       
   378 <div class="section" title="4.5. insert">
       
   379 <div class="titlepage"><div><div><h3 class="title">
       
   380 <a name="insert"></a>4.5. insert</h3></div></div></div>
       
   381 <p><a name="x_1b"></a>The 
       
   382         <span class="emphasis"><em>insert</em></span> method is used to insert an element specified as the second argument to the list at the position specified by the first argument:
       
   383       </p>
       
   384 <pre class="programlisting">&gt;&gt;&gt; a = ['Python', 'is', 'cool']
       
   385 &gt;&gt;&gt; a.insert(2, 'so')
       
   386 &gt;&gt;&gt; a
       
   387 ['Python', 'is', 'so', 'cool']
       
   388 
       
   389 </pre>
       
   390 <p><a name="x_1c"></a>The 
       
   391         <span class="emphasis"><em>insert</em></span> method changes the 
       
   392         <span class="emphasis"><em>List</em></span> in-place.
       
   393       </p>
       
   394 </div>
       
   395 <div class="section" title="4.6. pop">
       
   396 <div class="titlepage"><div><div><h3 class="title">
       
   397 <a name="pop"></a>4.6. pop</h3></div></div></div>
       
   398 <p><a name="x_1d"></a>The 
       
   399         <span class="emphasis"><em>pop</em></span> method removes an element from the list. The index position of the element to be removed can be specified as an argument to the 
       
   400         <span class="emphasis"><em>pop</em></span> method, if not it removes the last element by default:
       
   401       </p>
       
   402 <pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, 4, 5]
       
   403 &gt;&gt;&gt; a.pop()
       
   404 &gt;&gt;&gt; a
       
   405 5
       
   406 &gt;&gt;&gt; a.pop(2)
       
   407 &gt;&gt;&gt; a
       
   408 3
       
   409 
       
   410 </pre>
       
   411 <p><a name="x_1e"></a>The 
       
   412         <span class="emphasis"><em>pop</em></span> method changes the 
       
   413         <span class="emphasis"><em>List</em></span> in-place.
       
   414       </p>
       
   415 </div>
       
   416 <div class="section" title="4.7. remove">
       
   417 <div class="titlepage"><div><div><h3 class="title">
       
   418 <a name="remove"></a>4.7. remove</h3></div></div></div>
       
   419 <p><a name="x_1f"></a>The 
       
   420         <span class="emphasis"><em>remove</em></span> method removes the first occurence of an element supplied as a parameter:
       
   421       </p>
       
   422 <pre class="programlisting">&gt;&gt;&gt; a = [1, 2, 3, 4, 2, 5, 2]
       
   423 &gt;&gt;&gt; a.remove(2)
       
   424 &gt;&gt;&gt; a
       
   425 [1, 3, 4, 2, 5, 2]
       
   426 
       
   427 
       
   428 </pre>
       
   429 </div>
       
   430 <div class="section" title="4.8. reverse">
       
   431 <div class="titlepage"><div><div><h3 class="title">
       
   432 <a name="reverse"></a>4.8. reverse</h3></div></div></div>
       
   433 <p><a name="x_20"></a>The 
       
   434         <span class="emphasis"><em>reverse</em></span> method reverses elements in the list. It is important to note here that 
       
   435         <span class="emphasis"><em>reverse</em></span> method changes the list in-place and doesn't return any thing:
       
   436       </p>
       
   437 <pre class="programlisting">&gt;&gt;&gt; a = ['guido', 'alex', 'tim']
       
   438 &gt;&gt;&gt; a.reverse()
       
   439 &gt;&gt;&gt; a
       
   440 ['tim', 'alex', 'guido']
       
   441 
       
   442 
       
   443 </pre>
       
   444 </div>
       
   445 <div class="section" title="4.9. sort">
       
   446 <div class="titlepage"><div><div><h3 class="title">
       
   447 <a name="sort"></a>4.9. sort</h3></div></div></div>
       
   448 <p><a name="x_21"></a>The 
       
   449         <span class="emphasis"><em>sort</em></span> method is used to sort the elements of the list. The 
       
   450         <span class="emphasis"><em>sort</em></span> method also sorts in-place and does not return anything:
       
   451       </p>
       
   452 <pre class="programlisting">&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
       
   453 &gt;&gt;&gt; a.sort()
       
   454 &gt;&gt;&gt; a
       
   455 [1, 3, 4, 5, 7]
       
   456 
       
   457 </pre>
       
   458 <p><a name="x_22"></a>In addition to the sort method on a 
       
   459         <span class="emphasis"><em>List</em></span> object we can also use the built-in 
       
   460         <span class="strong"><strong>sorted</strong></span> function. This function takes the 
       
   461         <span class="emphasis"><em>List</em></span> as a parameter and returns a sorted copy of the list. However the original list is left intact:
       
   462       </p>
       
   463 <pre class="programlisting">&gt;&gt;&gt; a = [5, 1, 3, 7, 4]
       
   464 &gt;&gt;&gt; b = sorted(a)
       
   465 &gt;&gt;&gt; b
       
   466 [1, 3, 4, 5, 7]
       
   467 &gt;&gt;&gt; a
       
   468 [5, 1, 3, 7, 4]
       
   469 
       
   470 
       
   471 </pre>
       
   472 </div>
       
   473 </div>
       
   474 <div class="section" title="5. Tuples">
       
   475 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   476 <a name="tuples"></a>5. Tuples</h2></div></div></div>
       
   477 <p><a name="x_23"></a>
       
   478       <span class="emphasis"><em>Tuples</em></span> are sequences just like 
       
   479       <span class="emphasis"><em>Lists</em></span>, but they are immutable. In other words 
       
   480       <span class="emphasis"><em>Tuples</em></span> provides a way to represent a group of items, where the group of items cannot be changed in any way. The syntax of a 
       
   481       <span class="emphasis"><em>Tuple</em></span> is also very similar to 
       
   482       <span class="emphasis"><em>List</em></span>. A 
       
   483       <span class="emphasis"><em>Tuple</em></span> is represented with the list of items, called elements of the 
       
   484       <span class="emphasis"><em>Tuple</em></span> separated by comma, with the entire list being enclosed in parenthesis. It is not compulsory to use parenthesis around a 
       
   485       <span class="emphasis"><em>Tuple</em></span> but it may be necessary in some of the cases:
       
   486     </p>
       
   487 <pre class="programlisting">&gt;&gt;&gt; a = 1, 2, 3
       
   488 &gt;&gt;&gt; a
       
   489 (1, 2, 3)
       
   490 &gt;&gt;&gt; b = 1,
       
   491 &gt;&gt;&gt; b
       
   492 (1,)
       
   493 
       
   494 </pre>
       
   495 <p><a name="x_24"></a>It is interesting to note the second example. Just a value followed by a comma automatically makes that an element of a 
       
   496       <span class="emphasis"><em>Tuple</em></span> with only one element. It is also important to note that, irrespective of input having a parenthesis, the output always has a parenthesis.
       
   497     </p>
       
   498 <p><a name="x_25"></a>The first example is also known as 
       
   499       <span class="emphasis"><em>Tuple packing</em></span>, because values are being packed into a tuple. It is also possible to do 
       
   500       <span class="emphasis"><em>Tuple unpacking</em></span> which is more interesting. It is better to understand that by example. Say we have a co-ordinate pair from which we need to separate x and y co-ordinates:
       
   501     </p>
       
   502 <pre class="programlisting">&gt;&gt;&gt; a = (1, 2)
       
   503 &gt;&gt;&gt; x, y = a
       
   504 &gt;&gt;&gt; x
       
   505 1
       
   506 &gt;&gt;&gt; y
       
   507 2
       
   508 
       
   509 </pre>
       
   510 <p><a name="x_26"></a>      <span class="emphasis"><em>Tuple unpacking</em></span> also has several other use-cases of which the most interesting one is to swap the values of two variables. Using programming languages like C would require anywhere around 10 lines of code and an extra temporary variable to do this (including all the #include stuff). Python does it in the most intuitive way in just one line. Say we want to swap the co-ordinates in the above example:
       
   511     </p>
       
   512 <pre class="programlisting">&gt;&gt;&gt; x, y = y, x
       
   513 &gt;&gt;&gt; x
       
   514 2
       
   515 &gt;&gt;&gt; y
       
   516 1
       
   517 
       
   518 </pre>
       
   519 <div class="section" title="5.1. Common Tuple Operations">
       
   520 <div class="titlepage"><div><div><h3 class="title">
       
   521 <a name="common-tuple-operations"></a>5.1. Common Tuple Operations</h3></div></div></div>
       
   522 <p><a name="x_27"></a>There is no need to introduce all the 
       
   523         <span class="emphasis"><em>Tuple</em></span> operations again, since 
       
   524         <span class="emphasis"><em>Tuples</em></span> support the following operations that 
       
   525         <span class="emphasis"><em>List</em></span> supports in exactly the same way:
       
   526       </p>
       
   527 <div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
       
   528 <li class="listitem" style="list-style-type: *"><p><a name="x_28"></a>Indexing</p></li>
       
   529 <li class="listitem" style="list-style-type: *"><p><a name="x_29"></a>Concatenating</p></li>
       
   530 <li class="listitem" style="list-style-type: *"><p><a name="x_2a"></a>Slicing</p></li>
       
   531 <li class="listitem" style="list-style-type: *"><p><a name="x_2b"></a>Membership</p></li>
       
   532 <li class="listitem" style="list-style-type: *"><p><a name="x_2c"></a>Multiplication</p></li>
       
   533 <li class="listitem" style="list-style-type: *"><p><a name="x_2d"></a>Length, Maximum, Minimum</p></li>
       
   534 </ul></div></blockquote></div>
       
   535 <p><a name="x_2e"></a>The following examples illustrate the above operations:</p>
       
   536 <pre class="programlisting">&gt;&gt;&gt; a = (1, 2, 3, 4, 5, 6)
       
   537 &gt;&gt;&gt; a[5]
       
   538 6
       
   539 &gt;&gt;&gt; b = (7, 8, 9)
       
   540 &gt;&gt;&gt; a + b
       
   541 (1, 2, 3, 4, 5, 6, 7, 8, 9)
       
   542 &gt;&gt;&gt; a[3:5]
       
   543 (4, 5)
       
   544 &gt;&gt;&gt; 5 in a
       
   545 True
       
   546 &gt;&gt;&gt; c = (1,)
       
   547 &gt;&gt;&gt; c * 5
       
   548 (1, 1, 1, 1, 1)
       
   549 &gt;&gt;&gt; len(a)
       
   550 6
       
   551 &gt;&gt;&gt; max(a)
       
   552 6
       
   553 &gt;&gt;&gt; min(a)
       
   554 1
       
   555 
       
   556 </pre>
       
   557 <p><a name="x_2f"></a>However the following 
       
   558         <span class="emphasis"><em>List</em></span> operations are not supported by 
       
   559         <span class="emphasis"><em>Tuples</em></span> because 
       
   560         <span class="emphasis"><em>Tuples</em></span> cannot be changed once they are created:
       
   561       </p>
       
   562 <div class="blockquote"><blockquote class="blockquote"><div class="itemizedlist"><ul class="itemizedlist" type="*">
       
   563 <li class="listitem" style="list-style-type: *"><p><a name="x_30"></a>Changing elements</p></li>
       
   564 <li class="listitem" style="list-style-type: *"><p><a name="x_31"></a>Deleting elements</p></li>
       
   565 <li class="listitem" style="list-style-type: *"><p><a name="x_32"></a>Assigning to slices</p></li>
       
   566 </ul></div></blockquote></div>
       
   567 <p><a name="x_33"></a>Similarity to 
       
   568         <span class="emphasis"><em>Lists</em></span> leads to the questions like, why not 
       
   569         <span class="emphasis"><em>Lists</em></span> only? Why do we even want 
       
   570         <span class="emphasis"><em>Tuples</em></span>? Can we do the same with 
       
   571         <span class="emphasis"><em>Lists</em></span>? And the answer is 
       
   572         <span class="strong"><strong>Yes</strong></span> we can do it, but 
       
   573         <span class="emphasis"><em>Tuples</em></span> are helpful at times, like we can return Tuples from functions. They are also returned by some built-in functions and methods. And also there are some use cases like co-ordinate among other things. So 
       
   574         <span class="emphasis"><em>Tuples</em></span> are helpful.
       
   575       </p>
       
   576 </div>
       
   577 </div>
       
   578 <div class="section" title="6. Additional Syntax">
       
   579 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   580 <a name="additional-syntax"></a>6. Additional Syntax</h2></div></div></div>
       
   581 <p><a name="x_34"></a>The following additional syntax are introduced to make it easier to operate on 
       
   582       <span class="emphasis"><em>Lists</em></span>.
       
   583     </p>
       
   584 <div class="section" title="6.1. range()">
       
   585 <div class="titlepage"><div><div><h3 class="title">
       
   586 <a name="range"></a>6.1. range()</h3></div></div></div>
       
   587 <p><a name="x_35"></a>The 
       
   588         <span class="emphasis"><em>range</em></span> function takes at least one argument and 2 additional optional arguments. If two or more arguments are specified, the range function returns a list of natural numbers starting from the first argument passed to it to the second argument. The third argument, if specified is used as a step. Suppose only one argument is specified, then 
       
   589         <span class="emphasis"><em>range</em></span> function returns a list of natural numbers starting from 0 upto the argument specified:
       
   590       </p>
       
   591 <pre class="programlisting">&gt;&gt;&gt; range(5, 10, 2)
       
   592 [5, 7, 9]
       
   593 &gt;&gt;&gt; range(2, 15)
       
   594 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
       
   595 &gt;&gt;&gt; range(12)
       
   596 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
       
   597 
       
   598 </pre>
       
   599 </div>
       
   600 <div class="section" title="6.2. for">
       
   601 <div class="titlepage"><div><div><h3 class="title">
       
   602 <a name="for"></a>6.2. for</h3></div></div></div>
       
   603 <p><a name="x_36"></a>The 
       
   604         <span class="strong"><strong>for</strong></span> keyword is used as a part of the looping construct. Unlike for loops in other languages, Python's for is used to iterate through the elements of sequences like 
       
   605         <span class="emphasis"><em>Lists</em></span>, 
       
   606         <span class="emphasis"><em>Tuples</em></span>, 
       
   607         <span class="emphasis"><em>Dictionaries</em></span>, etc. The syntax of the for loop consists of 
       
   608         <span class="strong"><strong>for</strong></span>, followed by a variable to hold the individual or the current element of the list during iteration and 
       
   609         <span class="strong"><strong>in</strong></span>, followed by the sequence and a semicolon(':') The next line which is part of the 
       
   610         <span class="strong"><strong>for</strong></span> loop, i.e the statements that are part of the loop should start with a new intend:
       
   611       </p>
       
   612 <pre class="programlisting">&gt;&gt;&gt; names = ['Guido', 'Alex', 'Tim']
       
   613 &gt;&gt;&gt; for name in names:
       
   614 ...   print "Name =", name
       
   615 ... 
       
   616 Name = Guido
       
   617 Name = Alex
       
   618 Name = Tim
       
   619 
       
   620 
       
   621 </pre>
       
   622 </div>
       
   623 </div>
       
   624 <div class="section" title="7. Conclusion">
       
   625 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
       
   626 <a name="conclusion"></a>7. Conclusion</h2></div></div></div>
       
   627 <p><a name="x_37"></a>This section on 
       
   628       <span class="emphasis"><em>Lists</em></span> and 
       
   629       <span class="emphasis"><em>Tuples</em></span> introduces almost all the necessary machinary required to work on 
       
   630       <span class="emphasis"><em>Lists</em></span> and 
       
   631       <span class="emphasis"><em>Tuples</em></span>. Topics like how to use these data structures in bigger more useful programs will be introduced in the subsequent chapters.
       
   632     </p>
       
   633 </div>
       
   634 </div>
       
   635 <div class="navfooter">
       
   636 <hr>
       
   637 <table width="100%" summary="Navigation footer">
       
   638 <tr>
       
   639 <td width="40%" align="left">
       
   640 <a accesskey="p" href="index.html">Prev</a> </td>
       
   641 <td width="20%" align="center"> </td>
       
   642 <td width="40%" align="right"> </td>
       
   643 </tr>
       
   644 <tr>
       
   645 <td width="40%" align="left" valign="top">Chapter 1. List and Tuples </td>
       
   646 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
       
   647 <td width="40%" align="right" valign="top"> </td>
       
   648 </tr>
       
   649 </table>
       
   650 </div>
       
   651 </body>
       
   652 </html>