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