manipulating-lists/script.rst
changeset 312 8cb703eee88d
child 421 c4c5d1123f07
equal deleted inserted replaced
311:3f942b8d3f2f 312:8cb703eee88d
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. Clearly state the objectives of the LO (along with RBT level)
       
     5 
       
     6 .. Prerequisites
       
     7 .. -------------
       
     8 
       
     9 ..   1. getting started with lists
       
    10 ..   2. 
       
    11 ..   3. 
       
    12      
       
    13 .. Author              : Madhu
       
    14    Internal Reviewer   : 
       
    15    External Reviewer   :
       
    16    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
       
    17 
       
    18 Script
       
    19 ------
       
    20 
       
    21 {{{ Show the slide containing the title }}}
       
    22 
       
    23 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
       
    24 
       
    25 
       
    26 {{{ Show the slide containing the outline }}}
       
    27 
       
    28 We have already learnt a lot about Lists in Python. In this tutorial,
       
    29 we will learn more about advanced features of Lists in Python. We will
       
    30 see in detail how to concatenate two lists, slicing and striding of
       
    31 lists, methods to sort and reverse the list.
       
    32 
       
    33 {{{ Shift to terminal and start ipython }}}
       
    34 
       
    35 To begin with let us start ipython, by typing::
       
    36 
       
    37   ipython
       
    38 
       
    39 on the terminal
       
    40 
       
    41 We already know what Lists are in Python, how to access individual
       
    42 elements in the list and some of the functions that can be run on the
       
    43 lists like max, min, sum len and so on. Now let us learn some of the
       
    44 basic operations that can be performed on Lists.
       
    45 
       
    46 We already know how to access individual elements in a List. But what
       
    47 if we have a scenario where we need to get a part of the entire list
       
    48 or what we call as a slice of the list? Python supports slicing on
       
    49 lists. Let us say I have the list::
       
    50 
       
    51   primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
       
    52 
       
    53 To obtain the all the primes between 10 and 20 from the above list of
       
    54 primes we say::
       
    55 
       
    56   primes[4:8]
       
    57 
       
    58 This gives us all the elements in the list starting from the element
       
    59 with the index 4 which is 11 in our list upto the element with index 8
       
    60 in the list but not including the eigth element. So we obtain a slice
       
    61 starting from 11 upto 19th. It is a very important to remember that
       
    62 when ever we specify a range of elements in Python the start index is
       
    63 included and end index is not included. So in the above case, 11 which
       
    64 was the element with the index 4 was included but 23 which was the
       
    65 element with index 8 was excluded.
       
    66 
       
    67 Generalizing, we can obtain a slice of the list "p" from the index
       
    68 "start" upto the index "end" but excluding "end" with the following
       
    69 syntax
       
    70 
       
    71 {{{ Show the slide containing p[start:stop] }}}
       
    72 
       
    73 By default the slice fetches all the elements between start and stop
       
    74 including start but not stop. So as to say we obtain all the elements
       
    75 between start and stop in steps of one. Python also provides us the
       
    76 functionality to specify the steps in which the slice must be
       
    77 obtained. Say we have::
       
    78 
       
    79   num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
       
    80 
       
    81 If we want to obtain all the odd numbers less than 10 from the list
       
    82 "num" we have to start from element with index 1 upto the index 10 in
       
    83 steps of 2::
       
    84 
       
    85   num[1:10:2]
       
    86 
       
    87 So if we don't specify the step it is by default 1. Similary there are
       
    88 default values for start and stop indices as well. If we don't specify
       
    89 the start index it is implicitly taken as the first element of the
       
    90 list::
       
    91 
       
    92   num[:10]
       
    93 
       
    94 This gives us all the elements from the beginning upto the 10th
       
    95 element but not including the 10th element in the list "num". Similary
       
    96 if the stop index is not specified it is implicitly assumed to be the
       
    97 end of the list, including the last element of the list::
       
    98 
       
    99   num[10:]
       
   100 
       
   101 gives all the elements starting from the 10th element in the list
       
   102 "num" upto the final element including that last element. Now::
       
   103 
       
   104   num[::2]
       
   105 
       
   106 gives us all the even numbers in the list "num".
       
   107 
       
   108 The other basic operation that we can perform on list is concatenation
       
   109 of two or more lists. We can combine two lists by using the "plus"
       
   110 operator. Say we have
       
   111 
       
   112 {{{ Read as you type }}}::
       
   113 
       
   114   a = [1, 2, 3, 4]
       
   115   b = [4, 5, 6, 7]
       
   116   a + b
       
   117 
       
   118 When we concatenate lists using the "plus" operator we get a new
       
   119 list. We can store this list in a new variable::
       
   120 
       
   121   c = a + b
       
   122   c
       
   123 
       
   124 It is important to observe that the "plus" operator always returns a
       
   125 new list without touching anything in the existing lists which are the
       
   126 operands of the concatenation operation.
       
   127 
       
   128 We know that list is a collection of data. Whenever we have a
       
   129 collection we run into situations where we want to start the
       
   130 collection. Lists support sort method which sorts the list inplace::
       
   131 
       
   132   a = [5, 1, 6, 7, 7, 10]
       
   133   a.sort()
       
   134 
       
   135 Now the contents of the list "a" will be::
       
   136 
       
   137   a
       
   138   [1, 5, 6, 7, 7, 10]
       
   139 
       
   140 Since the sort method sorts the list inplace the original list we had
       
   141 is overwritten or replaced. We have no way to obtain the original list
       
   142 back. One way to avoid this is to keep a copy of the original list in
       
   143 another variable and run the sort method on the list. However Python
       
   144 also provides a built-in function called sorted which sorts the list
       
   145 which is passed as an argument to it and returns a new sorted list::
       
   146 
       
   147   a = [5, 1, 6, 7, 7, 10]
       
   148   sorted(a)
       
   149   
       
   150 We can store this sorted list another list variable::
       
   151 
       
   152   sa = sorted(a)
       
   153 
       
   154 Similarly to perform certain operations on the list we would like to
       
   155 reverse the list. Python provides reverse method which again reverses
       
   156 the list inplace::
       
   157 
       
   158   a = [1, 2, 3, 4, 5]
       
   159   a.reverse()
       
   160 
       
   161 reverses the list "a" and stores the reversed list inplace i.e. in "a"
       
   162 itself. Lets see the list "a"::
       
   163 
       
   164   a
       
   165   [5, 4, 3, 2, 1]
       
   166 
       
   167 But again the original list is lost. If we want to obtain the reverse
       
   168 of a list keeping the original list intact we can use the Python
       
   169 built-in function reversed. reversed function returns a new list which
       
   170 is the reverse of the list which was passed as the argument to the
       
   171 reversed function::
       
   172 
       
   173   a = [1, 2, 3, 4, 5]
       
   174   reversed(a)
       
   175 
       
   176 We can also store this new reversed list in another list variable.
       
   177 
       
   178 {{{ Show summary slide }}}
       
   179 
       
   180 This brings us to the end of another session. In this tutorial session
       
   181 we learnt
       
   182 
       
   183   * How to define strings
       
   184   * Different types of defining a string
       
   185   * String concatenation and repeatition
       
   186   * Accessing individual elements of the string
       
   187   * Immutability of strings
       
   188 
       
   189 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   190 
       
   191 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   192 
       
   193 Hope you have enjoyed and found it useful.
       
   194 Thank you!
       
   195  
       
   196