1 * Lists |
|
2 *** Outline |
|
3 ***** Lists |
|
4 ***** Tuples |
|
5 ***** Arsenal Required |
|
6 *** Script |
|
7 Welcome friends. |
|
8 |
|
9 In this tutorial we shall look at some special Data structures |
|
10 supported by Python namely Lists and Tuples. We have already been |
|
11 introduced to lists in some of the previous tutorials, here we |
|
12 shall look at them in little more detail. |
|
13 |
|
14 The list type is a container that holds a number of other |
|
15 objects, in the given order. Lists allow you to add and |
|
16 remove objects from the sequence. |
|
17 |
|
18 First lets start the interpreter by typing ipython in terminal. |
|
19 We create our first list by typing |
|
20 num = [1, 2, 3, 4] |
|
21 Items enclosed in square brackets separated by comma |
|
22 constitutes a list. |
|
23 One neat feature of Python list is that we can store data of any |
|
24 type in them. We can have a list something like: |
|
25 var = [1, 1.2, 'string'] |
|
26 print var |
|
27 |
|
28 Similar to strings, we can concatenate two lists using '+' |
|
29 operator |
|
30 so num + var will return a new list with the contents of both |
|
31 'num' and 'var' one after the other. |
|
32 Let's look at what num contains now |
|
33 print num |
|
34 As you can see num is unchanged by the '+' operator. |
|
35 |
|
36 We have already covered the append function in one of our previous |
|
37 tutorials. |
|
38 To add single object at the end of a list the 'append' |
|
39 function is used |
|
40 Let's now append -5 to it. |
|
41 num.append(-5) |
|
42 The contents of num have been changed now. |
|
43 print num |
|
44 append takes only one argument. And append behaves different |
|
45 from + operator. While + returns a new list with two lists |
|
46 added, append will simply add the entire object to the |
|
47 end of the list: |
|
48 num.append([9, 10, 11]) |
|
49 print num |
|
50 It adds the entire list as one element and not separate elements. |
|
51 In order to add separate elements we use the 'extend' function |
|
52 Let's reinitialize num |
|
53 num = [1, 4, -6] |
|
54 num.extend([2, 8, 0]) |
|
55 print num |
|
56 |
|
57 Let's now move on to see more functions available |
|
58 with lists. |
|
59 To reverse a list, we have the 'reverse' function. |
|
60 Please note the order of the elements in num. Let's now do: |
|
61 num.reverse() |
|
62 Now after using reverse function, lets check the value of 'num' |
|
63 print num |
|
64 Please note, the reverse() function actually manipulated the list. |
|
65 To remove a particular element from the list Python provides |
|
66 the remove() function |
|
67 num.remove(8) |
|
68 if the given argument is present more than once in the list, |
|
69 then the first occurrence of that element is removed from list. |
|
70 |
|
71 The Slicing and Striding concepts which we covered for Arrays work |
|
72 with lists as well. Lets revisit the concept by looking at some examples |
|
73 a = [1, 2, 3, 4, 5] |
|
74 print a[1:3] returns a list with second and third element of 'a' |
|
75 One important feature of list indexing is the negative index. In |
|
76 Lists -1 indicates last element of the list |
|
77 print a[-1] |
|
78 similarly -2 will be second last and so forth. Now these |
|
79 negative indexes can also be used with slicing. If we try |
|
80 print a[1:-1] |
|
81 we get list which excludes first and last element of a. |
|
82 and if we do not specify the start or the end index value the default |
|
83 values are taken. The default values being the first element and the |
|
84 last element. |
|
85 print a[:3] will return a list from beginning upto the fourth element of a. |
|
86 We can perform striding as well, by specifying the step size |
|
87 print a[1:-1:2] |
|
88 This gives second, fourth and so on items of a till we reach |
|
89 last item of list. |
|
90 print a[::2] will skip all the even placed elements of a |
|
91 With step sizes, if we specify negative values we get some |
|
92 interesting results. Lets try |
|
93 print a[4:1:-1] |
|
94 Here we begin at the 5th element and go upto the 2nd element in the |
|
95 reverse order since step size is -1 |
|
96 print a[::-1] |
|
97 This returns a slice with all the elements in 'a' reversed in order. |
|
98 Here the negative step indicates that the start point has to be the |
|
99 last element and the end point has to be the first element and the order |
|
100 has to be reversed. |
|
101 |
|
102 Let's now move on to other functionality |
|
103 We can check for containership of elements within lists as well. |
|
104 Let's look at the contents of num |
|
105 print num |
|
106 To check if the number 4 is present in the list we type |
|
107 4 in num |
|
108 True |
|
109 |
|
110 Now let's move onto Tuples. |
|
111 Python provides support for special immutable lists known as |
|
112 'tuple' To create a tuple instead we use normal brackets '(' |
|
113 unlike '[' for lists. |
|
114 t = (1, 2, 3, 4, 5, 6, 7, 8) |
|
115 its elements can also be accessed using indexes just like lists. |
|
116 print t[0] + t[3] + t[-1] |
|
117 but operation like |
|
118 t[4] = 7 are not allowed |
|
119 These features of tuples have their advantages. To see where |
|
120 are they used we first create two variables |
|
121 a, b = 1, 6 |
|
122 print a, b |
|
123 As you can see multiple variable assignments are possible using |
|
124 tuples. |
|
125 Now lets swap values their values. Normal approach would be |
|
126 to create a temporary to hold the value but because of tuples |
|
127 we can do something cool like |
|
128 b, a = a, b |
|
129 print a, b |
|
130 and values are swapped. And this swapping works for all types |
|
131 of variables. This is possible because of something magical |
|
132 that Python does called as tuple packing and unpacking. |
|
133 |
|
134 With this we come to the end of this tutorial on Lists and |
|
135 tuples. In this tutorial we have learnt about initializing, |
|
136 various list operations, slicing and striding. We learnt |
|
137 about tuple initialization, packing and unpacking. In next |
|
138 session we will cover more on Python supported data |
|
139 structures. Thank you! |
|
140 |
|
141 *** Notes |
|