22 |
23 |
23 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. |
24 Hello friends. Welcome to this spoken tutorial on Manipulating Lists. |
24 |
25 |
25 {{{ Show the slide containing the outline }}} |
26 {{{ Show the slide containing the outline }}} |
26 |
27 |
27 We have already learnt a lot about Lists in Python. In this tutorial, |
28 We have already learnt about Lists in Python. In this tutorial, |
28 we will learn more about advanced features of Lists in Python. We will |
29 we will learn about more advanced features of Lists in Python like how |
29 see how to concatenate two lists, details of slicing and striding of |
30 to concatenate two lists, details of slicing and striding of lists, |
30 lists, methods to sort and reverse lists. |
31 methods to sort and reverse lists. |
31 |
32 |
32 {{{ Shift to terminal and start ipython }}} |
33 {{{ Shift to terminal and start ipython }}} |
33 |
34 |
34 To begin with let us start ipython, by typing:: |
35 To begin with let us start ipython, by typing:: |
35 |
36 |
47 or what we call as a slice of the list? Python supports slicing on |
48 or what we call as a slice of the list? Python supports slicing on |
48 lists. Let us say I have the list:: |
49 lists. Let us say I have the list:: |
49 |
50 |
50 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
51 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
51 |
52 |
52 To obtain the all the primes between 10 and 20 from the above list of |
53 To obtain all the primes between 10 and 20 from the above list of |
53 primes we say:: |
54 primes we say:: |
54 |
55 |
55 primes[4:8] |
56 primes[4:8] |
56 |
57 |
57 This gives us all the elements in the list starting from the element |
58 This gives us all the elements in the list starting from the element |
58 with the index 4 which is 11 in our list upto the element with index 8 |
59 with the index 4, which is 11 in our list, upto the element with index 8 |
59 in the list but not including the eigth element. So we obtain a slice |
60 in the list but not including the eigth element. So we obtain a slice |
60 starting from 11 upto 19th. It is a very important to remember that |
61 starting from 11 upto 19th. It is a very important to remember that |
61 when ever we specify a range of elements in Python the start index is |
62 whenever we specify a range of elements in Python the start index is |
62 included and end index is not included. So in the above case, 11 which |
63 included and end index is not included. So in the above case, 11 which |
63 was the element with the index 4 was included but 23 which was the |
64 was the element with the index 4 was included but 23 which was the |
64 element with index 8 was excluded. |
65 element with index 8 was excluded. |
65 |
66 |
66 Following is an exercise you must do. |
67 Following is an exercise you must do. |
127 num[::3] |
128 num[::3] |
128 |
129 |
129 gives us all the multiples of 3 from the list, since every third |
130 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 element in it, starting from 0, is divisible by 3. |
131 |
132 |
132 The other basic operation that we can perform on list is concatenation |
133 The other basic operation that we can perform on lists is concatenation |
133 of two or more lists. We can combine two lists by using the "plus" |
134 of two or more lists. We can combine two lists by using the "plus" |
134 operator. Say we have |
135 operator. Say we have |
135 |
136 |
136 {{{ Read as you type }}}:: |
137 {{{ Read as you type }}}:: |
137 |
138 |
158 Now the contents of the list ``a`` will be:: |
159 Now the contents of the list ``a`` will be:: |
159 |
160 |
160 a |
161 a |
161 [1, 5, 6, 7, 7, 10] |
162 [1, 5, 6, 7, 7, 10] |
162 |
163 |
163 Since the sort method sorts the list inplace the original list we had |
164 As the sort method sorts the elements of a list, the original list we had |
164 is overwritten or replaced. We have no way to obtain the original list |
165 is overwritten or replaced. We have no way to obtain the original list |
165 back. One way to avoid this is to keep a copy of the original list in |
166 back. One way to avoid this is to keep a copy of the original list in |
166 another variable and run the sort method on the list. However Python |
167 another variable and run the sort method on the list. However Python |
167 also provides a built-in function called sorted which sorts the list |
168 also provides a built-in function called sorted which sorts the list |
168 which is passed as an argument to it and returns a new sorted list:: |
169 which is passed as an argument to it and returns a new sorted list:: |