1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
.. Objectives
.. ----------
.. Clearly state the objectives of the LO (along with RBT level)
.. Prerequisites
.. -------------
.. 1. getting started with lists
.. 2.
.. 3.
.. Author : Madhu
Internal Reviewer : Punch
External Reviewer :
Language Reviewer : Bhanukiran
Checklist OK? : <16-11-2010, Anand, OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello friends and Welcome to the tutorial on 'Manipulating Lists'.
.. L2
{{{ Show the slide containing objectives }}}
.. R2
At the end of this tutorial, you will be able to,
1. Concatenate two lists
#. Learn the details of slicing and striding of lists
#. Sort and reverse lists.
.. L3
{{{ Switch to the pre-requisite slide }}}
.. R3
Before beginning this tutorial,we would suggest you to complete the
tutorial on "Getting started with Lists".
.. L4
{{{ Open the terminal and start ipython }}}
::
ipython
.. R4
let us start ipython on our terminal
.. R5
We have already learnt about lists in Python, how to access individual
elements in the list and some of the functions that can be run on the
lists like ``max, min, sum, len`` and so on. Now let us learn some of
the basic operations that can be performed on Lists.
We already know how to access individual elements in a List. But what
if we have a scenario where we need to get a part of the entire list
or what we call as a slice of the list? Python supports slicing on
lists. Let us say I have the list,
.. L5
::
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
.. R6
To obtain all the primes between 10 and 20 from the above list of
primes we say
.. L6
::
primes[4:8]
.. R7
This gives us all the elements in the list starting from the element
with the index 4, which is 11, upto the element with index 8
in the list but not including the eighth element. So we obtain a slice
starting from 11 upto 19th. It is very important to remember that
whenever we specify a range of elements in Python, the start index is
included and end index is not included. So in the above case, 11, which
was the element with the index 4, was included but 23 which was the
element with index 8 was excluded.
Pause the video here, try out the following exercise and resume the video.
.. L7
.. L8
{{{ Show slide with exercise 1 }}}
.. R9
Obtain the primes less than 10, from the list ``primes``.
.. R10
Switch to the terminal for solution
.. L10
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
primes[0:4]
.. R11
It give us the primes below 10.
.. L11
.. L12
{{{ Show the slide containing p[start:stop] }}}
.. R12
Generalizing, we can obtain a slice of the list "p" from the index
"start" upto the index "end" but excluding "end" with the
syntax ``p[start:stop]``
.. L13
{{{ Switch to terminal }}}
.. R13
By default the slice fetches all the elements between start and stop
including start but not stop. So as to say we obtain all the elements
between start and stop in steps of one.
.. R14
Python also provides us the functionality to specify the steps in which
the slice must be obtained. Say we have
.. L14
::
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
.. R15
If we want to obtain all the odd numbers less than 10 from the list
``num`` we have to start from element with index 1 upto the index 10 in
steps of 2
.. L15
::
num[1:10:2]
.. R16
When no step is specified, it is assumed to be 1. Similarly, there are
default values for start and stop indices as well. If we don't specify
the start index, it is implicitly taken as the first element of the
list
.. L16
::
num[:10]
.. R17
This gives us all the elements from the beginning upto the 10th
element but not including the 10th element in the list. Similarly
if the stop index is not specified, it is implicitly assumed to be the
end of the list, including the last element of the list
.. L17
::
num[10:]
.. R18
This gives all the elements starting from the 10th element in the list
"num" upto the final element including that last element.
To get all the even numbers in the list "num", we do
.. L18
::
num[::2]
.. R19
Pause the video here, try out the following exercise and resume the video.
.. L19
.. L20
{{{ Show slide with exercise 2 }}}
.. R20
Obtain all the multiples of three from the list ``num``.
.. L21
{{{ Show slide with Solution 2 }}}
.. R21
The solution is on your screen.
``num[::3]`` gives us all the multiples of 3 from the list, since every
third element in it, starting from 0, is divisible by 3.
.. R22
The other basic operation that we can perform on lists is concatenation
of two or more lists. We can combine two lists by using the "plus"
operator. Say we have
.. L22
::
a = [1, 2, 3, 4]
b = [4, 5, 6, 7]
a + b
.. R23
When we concatenate lists using the "plus" operator we get a new
list. We can store this list in a new variable,say c,
.. L23
::
c = a + b
c
.. R24
It is important to observe that the "plus" operator always returns a
new list without altering the lists being concatenated in any way.
We know that a list is a collection of data. Whenever we have a
collection, we run into situations where we want to sort the
collection. Lists support ``sort`` method which sorts the list in place
.. L24
::
a = [5, 1, 6, 7, 7, 10]
a.sort()
.. R25
Now the contents of the list ``a`` will be
.. L25
::
a
.. R26
As the ``sort`` method sorts the elements of a list, the original list
we had, is overwritten or replaced. We have no way to obtain the
original list back. One way to avoid this is to keep a copy of the
original list in another variable and run the sort method on the list.
However Python also provides a built-in function called sorted which
sorts the list which is passed as an argument to it and returns a new
sorted list
.. L26
::
a = [5, 1, 6, 7, 7, 10]
sorted(a)
.. R27
We can store this sorted list into another list variable
.. L27
::
sa = sorted(a)
.. R28
Python also provides the ``reverse`` method which reverses
the list in place
.. L28
::
a = [1, 2, 3, 4, 5]
a.reverse()
.. R29
the ``reverse`` method reverses the list "a" and stores the reversed
list in place i.e. in "a" itself. Lets see the list "a"
.. L29
::
a
.. R30
But again the original list is lost.
To reverse a list, we could use striding with negative indexing.
.. L30
::
a[::-1]
.. R31
We can also store this new reversed list in another list variable.
Pause the video here, try out the following exercise and resume the video.
.. L31
.. L32
{{{ Show slide with exercise 3 }}}
.. R32
Given a list of marks of students in an examination, obtain a
list with marks in descending order.
marks = [99, 67, 47, 100, 50, 75, 62]
.. R33
Switch to terminal for solution.
.. L33
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
sorted(marks)[::-1]
.. R34
OR
.. L34
::
sorted(marks, reverse = True)
.. L35
{{{ Show summary slide }}}
.. R35
This brings us to the end of this tutorial. In this tutorial,
we have learnt to,
1. Obtain parts of lists using slicing and striding.
#. Concatenate lists using the ``plus`` operator.
#. Sort lists using the ``sort`` method.
#. Use the method ``reverse`` to reverse the lists.
.. L36
{{{Show self assessment questions slide}}}
.. R36
Here are some self assessment questions for you to solve
1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
29]``, How do you obtain the last 4 primes?
2. Given a list, p, of unknown length, obtain the first 3 (or all, if
there are fewer) characters of it.
3. ``reversed`` function reverses a list in place. True or False?
.. L37
{{{solution of self assessment questions on slide}}}
.. R37
And the answers,
1. The last four primes can be obtained from the given list as,
::
primes[-4:]
2. The first 3 characters can be obtained as,
::
p[:3]
3. False. The function ``reverse`` will reverse a list in place.
.. L38
{{{ Show the thank you slide }}}
.. R38
Hope you have enjoyed this tutorial and found it useful.
Thank you!
|