manipulating-lists/script.rst
author anand
Mon, 15 Nov 2010 15:08:22 +0530
changeset 506 ea74e6056453
parent 421 c4c5d1123f07
child 517 71697b10f4ae
permissions -rw-r--r--
checklist not ok; 2 long answer questions have to be added.
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
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
   Internal Reviewer   : 
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
   External Reviewer   :
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
Script
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
------
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
{{{ Show the slide containing the title }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
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
    24
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
{{{ Show the slide containing the outline }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
We have already learnt a lot about Lists in Python. In this tutorial,
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
we will learn more about advanced features of Lists in Python. We will
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    29
see how to concatenate two lists, details of slicing and striding of
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    30
lists, methods to sort and reverse lists.
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
{{{ Shift to terminal and start ipython }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
To begin with let us start ipython, by typing::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
  ipython
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
on the terminal
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
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
    41
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
    42
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
    43
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
    44
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
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
    46
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
    47
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
    48
lists. Let us say I have the list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
  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
    51
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
To obtain the all the primes between 10 and 20 from the above list of
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
primes we say::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
  primes[4:8]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
This gives us all the elements in the list starting from the element
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
with the index 4 which is 11 in our list upto the element with index 8
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
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
    60
starting from 11 upto 19th. It is a very important to remember that
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
when ever we specify a range of elements in Python the start index is
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
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
    63
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
    64
element with index 8 was excluded.
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    66
Following is an exercise you must do. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    67
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    68
%% %% Obtain the primes less than 10, from the list ``primes``. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    69
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    70
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
    71
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
  primes[0:4]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    75
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    76
will give us the primes below 10. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    77
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
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
    79
"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
    80
syntax
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
{{{ Show the slide containing p[start:stop] }}}
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
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
    85
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
    86
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
    87
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
    88
obtained. Say we have::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
  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
    91
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
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
    93
``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
    94
steps of 2::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
  num[1:10:2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
    98
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
    99
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
   100
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
   101
list::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
  num[:10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
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
   106
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
   107
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
   108
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
   109
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
  num[10:]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
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
   113
"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
   114
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
  num[::2]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
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
   118
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   119
Following is an exercise that you must do. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   120
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   121
%% %% Obtain all the multiples of three from the list ``num``.
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   122
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   123
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
   124
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
  num[::3]
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   128
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   129
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
   130
element in it, starting from 0, is divisible by 3. 
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   131
312
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
The other basic operation that we can perform on list is concatenation
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
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
   134
operator. Say we have
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
{{{ Read as you type }}}::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
  a = [1, 2, 3, 4]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
  b = [4, 5, 6, 7]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
  a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
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
   143
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
   144
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
  c = a + b
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  c
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
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
   149
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
   150
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   151
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
   152
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
   153
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
   154
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
  a.sort()
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
421
c4c5d1123f07 Cleaned up manipulating lists.
Puneeth Chaganti <punchagan@fossee.in>
parents: 312
diff changeset
   158
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
   159
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
  a
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
  [1, 5, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
Since the sort method sorts the list inplace the original list we had
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
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
   165
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
   166
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
   167
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
   168
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
   169
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
  a = [5, 1, 6, 7, 7, 10]
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
  
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
We can store this sorted list another list variable::
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
  sa = sorted(a)
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
Similarly to perform certain operations on the list we would like to
8cb703eee88d Manipulating lists LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
reverse the list. Python provides reverse method which again reverses
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