manipulating-lists/script.rst
author anand
Tue, 16 Nov 2010 23:28:06 +0530
changeset 518 33ad94cee1fb
parent 517 71697b10f4ae
permissions -rw-r--r--
checklist ok for `manipulating strings`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. Clearly state the objectives of the LO (along with RBT level)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
.. Prerequisites
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
.. -------------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
..   1. getting started with lists
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
..   2. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
..   3. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
     
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
.. Author              : Madhu
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    14
   Internal Reviewer   : Punch
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
   External Reviewer   :
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    16
   Language Reviewer   : Bhanukiran
518
33ad94cee1fb checklist ok for `manipulating strings`
anand
parents: 517
diff changeset
    17
   Checklist OK?       : <16-11-2010, Anand,  OK> [2010-10-05]
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
Script
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
------
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
{{{ Show the slide containing the title }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
{{{ Show the slide containing the outline }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    28
We have already learnt about Lists in Python. In this tutorial,
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    29
we will learn about more advanced features of Lists in Python like how
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    30
to concatenate two lists, details of slicing and striding of lists, 
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    31
methods to sort and reverse lists.
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
{{{ Shift to terminal and start ipython }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
To begin with let us start ipython, by typing::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
  ipython
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
on the terminal
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
We already know what Lists are in Python, how to access individual
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
elements in the list and some of the functions that can be run on the
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    43
lists like ``max, min, sum, len`` and so on. Now let us learn some of
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    44
the basic operations that can be performed on Lists.
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
We already know how to access individual elements in a List. But what
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
if we have a scenario where we need to get a part of the entire list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
or what we call as a slice of the list? Python supports slicing on
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
lists. Let us say I have the list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
  primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    53
To obtain all the primes between 10 and 20 from the above list of
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
primes we say::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
  primes[4:8]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
This gives us all the elements in the list starting from the element
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    59
with the index 4, which is 11 in our list, upto the element with index 8
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
in the list but not including the eigth element. So we obtain a slice
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
starting from 11 upto 19th. It is a very important to remember that
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
    62
whenever we specify a range of elements in Python the start index is
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
included and end index is not included. So in the above case, 11 which
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
was the element with the index 4 was included but 23 which was the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
element with index 8 was excluded.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    67
Following is an exercise you must do. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    68
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    69
%% %% Obtain the primes less than 10, from the list ``primes``. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    70
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    71
Please, pause the video here, do the exercise and then resume. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    72
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    73
::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    74
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    75
  primes[0:4]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    76
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    77
will give us the primes below 10. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    78
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
Generalizing, we can obtain a slice of the list "p" from the index
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
"start" upto the index "end" but excluding "end" with the following
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
syntax
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
{{{ Show the slide containing p[start:stop] }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
By default the slice fetches all the elements between start and stop
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
including start but not stop. So as to say we obtain all the elements
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
between start and stop in steps of one. Python also provides us the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
functionality to specify the steps in which the slice must be
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
obtained. Say we have::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
  num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
If we want to obtain all the odd numbers less than 10 from the list
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    94
``num`` we have to start from element with index 1 upto the index 10 in
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
steps of 2::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
  num[1:10:2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    99
When no step is specified, it is assumed to be 1. Similarly, there are
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
default values for start and stop indices as well. If we don't specify
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
the start index it is implicitly taken as the first element of the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
  num[:10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
This gives us all the elements from the beginning upto the 10th
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
element but not including the 10th element in the list "num". Similary
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
if the stop index is not specified it is implicitly assumed to be the
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
end of the list, including the last element of the list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
  num[10:]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
gives all the elements starting from the 10th element in the list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
"num" upto the final element including that last element. Now::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
  num[::2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
gives us all the even numbers in the list "num".
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   120
Following is an exercise that you must do. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   121
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   122
%% %% Obtain all the multiples of three from the list ``num``.
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   123
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   124
Please, pause the video here. Do the exercise and then continue. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   125
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   126
::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   127
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   128
  num[::3]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   129
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   130
gives us all the multiples of 3 from the list, since every third
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   131
element in it, starting from 0, is divisible by 3. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   132
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
   133
The other basic operation that we can perform on lists is concatenation
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
of two or more lists. We can combine two lists by using the "plus"
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
operator. Say we have
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
{{{ Read as you type }}}::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
  a = [1, 2, 3, 4]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
  b = [4, 5, 6, 7]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
  a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
When we concatenate lists using the "plus" operator we get a new
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
list. We can store this list in a new variable::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  c = a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
  c
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
It is important to observe that the "plus" operator always returns a
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   150
new list without altering the lists being concatenated in any way. 
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   152
We know that a list is a collection of data. Whenever we have a
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   153
collection we run into situations where we want to sort the
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
collection. Lists support sort method which sorts the list inplace::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
  a.sort()
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   159
Now the contents of the list ``a`` will be::
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
  a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
  [1, 5, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
   164
As the sort method sorts the elements of a list, the original list we had
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
is overwritten or replaced. We have no way to obtain the original list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
back. One way to avoid this is to keep a copy of the original list in
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
another variable and run the sort method on the list. However Python
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
also provides a built-in function called sorted which sorts the list
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
which is passed as an argument to it and returns a new sorted list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
  sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
  
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
We can store this sorted list another list variable::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
  sa = sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 421
diff changeset
   178
Python also provides the reverse method which reverses
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
the list inplace::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
  a = [1, 2, 3, 4, 5]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
  a.reverse()
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
reverses the list "a" and stores the reversed list inplace i.e. in "a"
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
itself. Lets see the list "a"::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
  a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
  [5, 4, 3, 2, 1]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   190
But again the original list is lost. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   191
.. #[punch: removed reversed, since it returns an iterator]
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   193
To reverse a list, we could use striding with negative indexing.::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   194
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   195
   a[::-1]
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
We can also store this new reversed list in another list variable.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   199
Following is an (are) exercise(s) that you must do. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   200
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   201
%% %% Given a list of marks of students in an examination, obtain a
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   202
      list with marks in descending order.
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   203
      ::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   204
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   205
            marks = [99, 67, 47, 100, 50, 75, 62]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   206
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   207
Please, pause the video here. Do the exercise(s) and then continue. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   208
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   209
::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   210
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   211
  sorted(marks)[::-1]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   212
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   213
OR
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   214
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   215
::
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   216
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   217
  sorted(marks, reverse = True)
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   218
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   219
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   220
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   221
{{{ Show summary slide }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
This brings us to the end of another session. In this tutorial session
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
we learnt
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   226
  * Obtaining parts of lists using slicing and striding
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   227
  * List concatenation
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   228
  * Sorting lists 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   229
  * Reversing lists
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   231
{{{ Show the "sponsored by FOSSEE" slide }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   232
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   234
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
Hope you have enjoyed and found it useful.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
Thank you!
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238