manipulating-lists/script.rst
changeset 421 c4c5d1123f07
parent 312 8cb703eee88d
child 517 71697b10f4ae
equal deleted inserted replaced
413:da1f270b07b7 421:c4c5d1123f07
    20 
    20 
    21 {{{ Show the slide containing the title }}}
    21 {{{ Show the slide containing the title }}}
    22 
    22 
    23 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
    23 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
    24 
    24 
    25 
       
    26 {{{ Show the slide containing the outline }}}
    25 {{{ Show the slide containing the outline }}}
    27 
    26 
    28 We have already learnt a lot about Lists in Python. In this tutorial,
    27 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
    28 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
    29 see how to concatenate two lists, details of slicing and striding of
    31 lists, methods to sort and reverse the list.
    30 lists, methods to sort and reverse lists.
    32 
    31 
    33 {{{ Shift to terminal and start ipython }}}
    32 {{{ Shift to terminal and start ipython }}}
    34 
    33 
    35 To begin with let us start ipython, by typing::
    34 To begin with let us start ipython, by typing::
    36 
    35 
    38 
    37 
    39 on the terminal
    38 on the terminal
    40 
    39 
    41 We already know what Lists are in Python, how to access individual
    40 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
    41 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
    42 lists like ``max, min, sum, len`` and so on. Now let us learn some of
    44 basic operations that can be performed on Lists.
    43 the basic operations that can be performed on Lists.
    45 
    44 
    46 We already know how to access individual elements in a List. But what
    45 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
    46 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
    47 or what we call as a slice of the list? Python supports slicing on
    49 lists. Let us say I have the list::
    48 lists. Let us say I have the list::
    62 when ever we specify a range of elements in Python the start index is
    61 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
    62 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
    63 was the element with the index 4 was included but 23 which was the
    65 element with index 8 was excluded.
    64 element with index 8 was excluded.
    66 
    65 
       
    66 Following is an exercise you must do. 
       
    67 
       
    68 %% %% Obtain the primes less than 10, from the list ``primes``. 
       
    69 
       
    70 Please, pause the video here, do the exercise and then resume. 
       
    71 
       
    72 ::
       
    73 
       
    74   primes[0:4]
       
    75 
       
    76 will give us the primes below 10. 
       
    77 
    67 Generalizing, we can obtain a slice of the list "p" from the index
    78 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
    79 "start" upto the index "end" but excluding "end" with the following
    69 syntax
    80 syntax
    70 
    81 
    71 {{{ Show the slide containing p[start:stop] }}}
    82 {{{ Show the slide containing p[start:stop] }}}
    77 obtained. Say we have::
    88 obtained. Say we have::
    78 
    89 
    79   num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    90   num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    80 
    91 
    81 If we want to obtain all the odd numbers less than 10 from the list
    92 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
    93 ``num`` we have to start from element with index 1 upto the index 10 in
    83 steps of 2::
    94 steps of 2::
    84 
    95 
    85   num[1:10:2]
    96   num[1:10:2]
    86 
    97 
    87 So if we don't specify the step it is by default 1. Similary there are
    98 When no step is specified, it is assumed to be 1. Similarly, there are
    88 default values for start and stop indices as well. If we don't specify
    99 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
   100 the start index it is implicitly taken as the first element of the
    90 list::
   101 list::
    91 
   102 
    92   num[:10]
   103   num[:10]
   103 
   114 
   104   num[::2]
   115   num[::2]
   105 
   116 
   106 gives us all the even numbers in the list "num".
   117 gives us all the even numbers in the list "num".
   107 
   118 
       
   119 Following is an exercise that you must do. 
       
   120 
       
   121 %% %% Obtain all the multiples of three from the list ``num``.
       
   122 
       
   123 Please, pause the video here. Do the exercise and then continue. 
       
   124 
       
   125 ::
       
   126 
       
   127   num[::3]
       
   128 
       
   129 gives us all the multiples of 3 from the list, since every third
       
   130 element in it, starting from 0, is divisible by 3. 
       
   131 
   108 The other basic operation that we can perform on list is concatenation
   132 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"
   133 of two or more lists. We can combine two lists by using the "plus"
   110 operator. Say we have
   134 operator. Say we have
   111 
   135 
   112 {{{ Read as you type }}}::
   136 {{{ Read as you type }}}::
   120 
   144 
   121   c = a + b
   145   c = a + b
   122   c
   146   c
   123 
   147 
   124 It is important to observe that the "plus" operator always returns a
   148 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
   149 new list without altering the lists being concatenated in any way. 
   126 operands of the concatenation operation.
   150 
   127 
   151 We know that a list is a collection of data. Whenever we have a
   128 We know that list is a collection of data. Whenever we have a
   152 collection we run into situations where we want to sort the
   129 collection we run into situations where we want to start the
       
   130 collection. Lists support sort method which sorts the list inplace::
   153 collection. Lists support sort method which sorts the list inplace::
   131 
   154 
   132   a = [5, 1, 6, 7, 7, 10]
   155   a = [5, 1, 6, 7, 7, 10]
   133   a.sort()
   156   a.sort()
   134 
   157 
   135 Now the contents of the list "a" will be::
   158 Now the contents of the list ``a`` will be::
   136 
   159 
   137   a
   160   a
   138   [1, 5, 6, 7, 7, 10]
   161   [1, 5, 6, 7, 7, 10]
   139 
   162 
   140 Since the sort method sorts the list inplace the original list we had
   163 Since the sort method sorts the list inplace the original list we had
   162 itself. Lets see the list "a"::
   185 itself. Lets see the list "a"::
   163 
   186 
   164   a
   187   a
   165   [5, 4, 3, 2, 1]
   188   [5, 4, 3, 2, 1]
   166 
   189 
   167 But again the original list is lost. If we want to obtain the reverse
   190 But again the original list is lost. 
   168 of a list keeping the original list intact we can use the Python
   191 .. #[punch: removed reversed, since it returns an iterator]
   169 built-in function reversed. reversed function returns a new list which
   192 
   170 is the reverse of the list which was passed as the argument to the
   193 To reverse a list, we could use striding with negative indexing.::
   171 reversed function::
   194 
   172 
   195    a[::-1]
   173   a = [1, 2, 3, 4, 5]
       
   174   reversed(a)
       
   175 
   196 
   176 We can also store this new reversed list in another list variable.
   197 We can also store this new reversed list in another list variable.
       
   198 
       
   199 Following is an (are) exercise(s) that you must do. 
       
   200 
       
   201 %% %% Given a list of marks of students in an examination, obtain a
       
   202       list with marks in descending order.
       
   203       ::
       
   204 
       
   205             marks = [99, 67, 47, 100, 50, 75, 62]
       
   206 
       
   207 Please, pause the video here. Do the exercise(s) and then continue. 
       
   208 
       
   209 ::
       
   210 
       
   211   sorted(marks)[::-1]
       
   212 
       
   213 OR
       
   214 
       
   215 ::
       
   216 
       
   217   sorted(marks, reverse = True)
       
   218 
       
   219 
   177 
   220 
   178 {{{ Show summary slide }}}
   221 {{{ Show summary slide }}}
   179 
   222 
   180 This brings us to the end of another session. In this tutorial session
   223 This brings us to the end of another session. In this tutorial session
   181 we learnt
   224 we learnt
   182 
   225 
   183   * How to define strings
   226   * Obtaining parts of lists using slicing and striding
   184   * Different types of defining a string
   227   * List concatenation
   185   * String concatenation and repeatition
   228   * Sorting lists 
   186   * Accessing individual elements of the string
   229   * Reversing lists
   187   * Immutability of strings
       
   188 
   230 
   189 {{{ Show the "sponsored by FOSSEE" slide }}}
   231 {{{ Show the "sponsored by FOSSEE" slide }}}
   190 
   232 
   191 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   233 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   192 
   234