|
1 Lists and Tuples |
|
2 ================ |
|
3 |
|
4 Python provides an intuitive way to represent a group items, called *Lists*. The |
|
5 items of a *List* are called its elements. Unlike C/C++, elements can be of any |
|
6 type. A *List* is represented as a list of comma-sepated elements with paren- |
|
7 thesis around them:: |
|
8 |
|
9 >>> a = [10, 'Python programming', 20.3523, 23, 3534534L] |
|
10 >>> a |
|
11 [10, 'Python programming', 20.3523, 23, 3534534L] |
|
12 |
|
13 |
|
14 Common List Operations |
|
15 ---------------------- |
|
16 |
|
17 |
|
18 Indexing |
|
19 ~~~~~~~~ |
|
20 |
|
21 Individual elements of a *List* can be accessed using an index to the element. |
|
22 The indices start at 0. One can also access the elements of the *List* in reverse |
|
23 using negative indices.:: |
|
24 |
|
25 >>> a[1] |
|
26 'Python programming' |
|
27 >>> a[-1] |
|
28 3534534L |
|
29 |
|
30 It is important to note here that the last element of the *List* has an index of |
|
31 -1. |
|
32 |
|
33 |
|
34 Concatenating |
|
35 ~~~~~~~~~~~~~ |
|
36 |
|
37 Two or more *Lists* can be concatenated using the + operator:: |
|
38 |
|
39 >>> a + ['foo', 12, 23.3432, 54] |
|
40 [10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54] |
|
41 >>> [54, 75, 23] + ['write', 67, 'read'] |
|
42 [54, 75, 23, 'write', 67, 'read'] |
|
43 |
|
44 |
|
45 Slicing |
|
46 ~~~~~~~ |
|
47 |
|
48 A *List* can be sliced off to contain a subset of elements of the *List*. Slicing |
|
49 can be done by using two indices separated by a colon, where the first index is |
|
50 inclusive and the second index is exclusive. The resulting slice is also a *List*.:: |
|
51 |
|
52 >>> num = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
53 >>> num[3:6] |
|
54 [4, 5, 6] |
|
55 >>> num[0:1] |
|
56 [1] |
|
57 >>> num[7:10] |
|
58 [7, 8, 9] |
|
59 |
|
60 The last example showed how to access last 3 elements of the *List*. There is a |
|
61 small catch here. The second index 10 actually refers to the 11th element of the |
|
62 *List* which is still valid, even though it doesn't exist because the second |
|
63 index is exclusive and tells the Python interpreter to get the last element of |
|
64 the *List*. But this can also be done in a much easier way using negative indices:: |
|
65 |
|
66 >>> num[-3:-1] |
|
67 [7, 8, 9] |
|
68 |
|
69 Excluding the first index implies that the slice must start at the beginning of |
|
70 the *List*, while excluding the second index includes all the elements till the |
|
71 end of the *List*. A third parameter to a slice, which is implicitly taken as 1 |
|
72 is the step of the slice. It is specified as a value which follows a colon after |
|
73 the second index:: |
|
74 |
|
75 >>> num[:4] |
|
76 [1, 2, 3, 4] |
|
77 >>> num[7:] |
|
78 [8, 9] |
|
79 >>> num[-3:] |
|
80 [7, 8, 9] |
|
81 >>> num[:] |
|
82 [1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
83 >>> num[4:9:3] |
|
84 [5, 8] |
|
85 >>> num[3::2] |
|
86 [4, 6, 8] |
|
87 >>> num[::4] |
|
88 [1, 5, 9] |
|
89 |
|
90 |
|
91 Multiplication |
|
92 ~~~~~~~~~~~~~~ |
|
93 |
|
94 A *List* can be multiplied with an integer to repeat itself:: |
|
95 |
|
96 >>> [20] * 5 |
|
97 [20, 20, 20, 20, 20] |
|
98 >>> [42, 'Python', 54] * 3 |
|
99 [42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54] |
|
100 |
|
101 |
|
102 Membership |
|
103 ~~~~~~~~~~ |
|
104 |
|
105 **in** operator is used to find whether an element is part of the *List*. It |
|
106 returns **True** if the element is present in the *List* or **False** if it is not |
|
107 present. Since this operator returns a Boolean value it is called a Boolean |
|
108 operator:: |
|
109 |
|
110 >>> names = ['Guido', 'Alex', 'Tim'] |
|
111 >>> 'Tim' in names |
|
112 True |
|
113 >>> 'Adam' in names |
|
114 False |
|
115 |
|
116 |
|
117 Length, Maximum and Minimum |
|
118 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
119 |
|
120 Length of a *List* can be found out using the len function. The max function |
|
121 returns the element with the largest value and the min function returns the |
|
122 element with the smallest value:: |
|
123 |
|
124 >>> num = [4, 1, 32, 12, 67, 34, 65] |
|
125 >>> len(num) |
|
126 7 |
|
127 >>> max(num) |
|
128 67 |
|
129 >>> min(num) |
|
130 1 |
|
131 |
|
132 |
|
133 Changing Elements |
|
134 ~~~~~~~~~~~~~~~~~ |
|
135 |
|
136 Unlike Strings *Lists* are mutable, i.e. elements of a *List* can be manipulated:: |
|
137 |
|
138 >>> a = [1, 3, 5, 7] |
|
139 >>> a[2] = 9 |
|
140 >>> a |
|
141 [1, 3, 9, 7] |
|
142 |
|
143 |
|
144 Deleting Elements |
|
145 ~~~~~~~~~~~~~~~~~ |
|
146 |
|
147 An element or a slice of a *List* can be deleted by using the **del** statement:: |
|
148 |
|
149 >>> a = [1, 3, 5, 7, 9, 11] |
|
150 >>> del a[-2:] |
|
151 >>> a |
|
152 [1, 3, 5, 7] |
|
153 >>> del a[1] |
|
154 >>> a |
|
155 [1, 5, 7] |
|
156 |
|
157 |
|
158 Assign to Slices |
|
159 ~~~~~~~~~~~~~~~~ |
|
160 |
|
161 In the same way, values can be assigned to individual elements of the *List*, |
|
162 a *List* of elements can be assigned to a slice:: |
|
163 |
|
164 >>> a = [2, 3, 4, 5] |
|
165 >>> a[:2] = [0, 1] |
|
166 [0, 1, 4, 5] |
|
167 >>> a[2:2] = [2, 3] |
|
168 >>> a |
|
169 [0, 1, 2, 3, 4, 5] |
|
170 >>> a[2:4] = [] |
|
171 >>> a |
|
172 [0, 1, 4, 5] |
|
173 |
|
174 The last two examples should be particularly noted carefully. The last but one |
|
175 example insert elements or a list of elements into a *List* and the last example |
|
176 deletes a list of elements from the *List*. |
|
177 |
|
178 |
|
179 None, Empty Lists, and Initialization |
|
180 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
181 |
|
182 An *Empty List* is a *List* with no elements and is simply represented as |
|
183 []. A *None List* is one with all elements in it being **None**. It serves |
|
184 the purpose having a container list of some fixed number of elements with |
|
185 no value:: |
|
186 |
|
187 >>> a = [] |
|
188 >>> a |
|
189 [] |
|
190 >>> n = [None] * 10 |
|
191 >>> n |
|
192 [None, None, None, None, None, None, None, None, None, None] |
|
193 |