175 example insert elements or a list of elements into a *List* and the last example |
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*. |
176 deletes a list of elements from the *List*. |
177 |
177 |
178 |
178 |
179 None, Empty Lists, and Initialization |
179 None, Empty Lists, and Initialization |
180 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
180 ------------------------------------- |
181 |
181 |
182 An *Empty List* is a *List* with no elements and is simply represented as |
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 |
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 |
184 the purpose having a container list of some fixed number of elements with |
185 no value:: |
185 no value:: |
189 [] |
189 [] |
190 >>> n = [None] * 10 |
190 >>> n = [None] * 10 |
191 >>> n |
191 >>> n |
192 [None, None, None, None, None, None, None, None, None, None] |
192 [None, None, None, None, None, None, None, None, None, None] |
193 |
193 |
|
194 |
|
195 Nested Lists |
|
196 ------------ |
|
197 |
|
198 As mentioned earlier, a List can contain elements of any data type. This also |
|
199 implies a *List* can have a *Lists* themselves as its elements. These are |
|
200 called as *Nested Lists*. There is no limit on the depth of the *Nested Lists*:: |
|
201 |
|
202 >>> a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7] |
|
203 |
|
204 |
|
205 List Methods |
|
206 ------------ |
|
207 |
|
208 A method is a function that is coupled to an object. More about objects |
|
209 and its methods are discussed in Advanced Python module. In general, a |
|
210 method is called like:: |
|
211 |
|
212 object.method(arguments) |
|
213 |
|
214 For now, it is enough to know that a list of elements is an object and |
|
215 so *List* methods can be called upon them. Also some of the methods change |
|
216 the *List* in-place, meaning it modifies the existing list instead of creating |
|
217 a new one, while other methods don't. It must be noted as we run through |
|
218 the *List* methods. |
|
219 |
|
220 Some of the most commonly used *List* methods are as follows: |
|
221 |
|
222 |
|
223 append |
|
224 ~~~~~~ |
|
225 |
|
226 The *append* method is used to append an object at the end of the list:: |
|
227 |
|
228 >>> prime = [2, 3, 5] |
|
229 >>> prime.append(7) |
|
230 >>> prime |
|
231 [2, 3, 5, 7] |
|
232 |
|
233 It is important to note that append changes the *List* in-place. |
|
234 |
|
235 |
|
236 count |
|
237 ~~~~~ |
|
238 |
|
239 The *count* method returns the number of occurences of a particular element |
|
240 in a list:: |
|
241 |
|
242 >>> [1, 4, 4, 9, 9, 9].count(9) |
|
243 3 |
|
244 >>> tlst = ['Python', 'is', 'a', 'beautiful', 'language'] |
|
245 >>> tlst.count('Python') |
|
246 1 |
|
247 |
|
248 |
|
249 extend |
|
250 ~~~~~~ |
|
251 |
|
252 The *extend* method extends the list on which it is called by the list supplied |
|
253 as argument to it:: |
|
254 |
|
255 >>> a = [1, 2, 3] |
|
256 >>> b = [4, 5, 6] |
|
257 >>> a.extend(b) |
|
258 [1, 2, 3, 4, 5, 6] |
|
259 |
|
260 This is an in-place method. This method is equivalent to using the + operator, but |
|
261 using the + operator returns a new list. |
|
262 |
|
263 |
|
264 index |
|
265 ~~~~~ |
|
266 |
|
267 The *index* method returns the index position of the element in the list |
|
268 specified as argument:: |
|
269 |
|
270 >>> a = [1, 2, 3, ,4, 5] |
|
271 >>> a.index(4) |
|
272 3 |
|
273 |
|
274 |
|
275 insert |
|
276 ~~~~~~ |
|
277 |
|
278 The *insert* method is used to insert an element specified as the second |
|
279 argument to the list at the position specified by the first argument:: |
|
280 |
|
281 >>> a = ['Python', 'is', 'cool'] |
|
282 >>> a.insert(2, 'so') |
|
283 >>> a |
|
284 ['Python', 'is', 'so', 'cool'] |
|
285 |
|
286 The *insert* method changes the *List* in-place. |
|
287 |
|
288 |
|
289 pop |
|
290 ~~~ |
|
291 |
|
292 The *pop* method removes an element from the list. The index position |
|
293 of the element to be removed can be specified as an argument to the |
|
294 *pop* method, if not it removes the last element by default:: |
|
295 |
|
296 >>> a = [1, 2, 3, 4, 5] |
|
297 >>> a.pop() |
|
298 >>> a |
|
299 5 |
|
300 >>> a.pop(2) |
|
301 >>> a |
|
302 3 |
|
303 |
|
304 The *pop* method changes the *List* in-place. |
|
305 |
|
306 |
|
307 remove |
|
308 ~~~~~~ |
|
309 |
|
310 The *remove* method removes the first occurence of an element supplied as a |
|
311 parameter:: |
|
312 |
|
313 >>> a = [1, 2, 3, 4, 2, 5, 2] |
|
314 >>> a.remove(2) |
|
315 >>> a |
|
316 [1, 3, 4, 2, 5, 2] |
|
317 |
|
318 |
|
319 reverse |
|
320 ~~~~~~~ |
|
321 |
|
322 The *reverse* method reverses elements in the list. It is important to note |
|
323 here that *reverse* method changes the list in-place and doesn't return any |
|
324 thing:: |
|
325 |
|
326 >>> a = ['guido', 'alex', 'tim'] |
|
327 >>> a.reverse() |
|
328 >>> a |
|
329 ['tim', 'alex', 'guido'] |
|
330 |
|
331 |
|
332 sort |
|
333 ~~~~ |
|
334 |
|
335 The *sort* method is used to sort the elements of the list. The *sort* method |
|
336 also sorts in-place and does not return anything:: |
|
337 |
|
338 >>> a = [5, 1, 3, 7, 4] |
|
339 >>> a.sort() |
|
340 >>> a |
|
341 [1, 3, 4, 5, 7] |
|
342 |
|
343 In addition to the sort method on a *List* object we can also use the built-in |
|
344 **sorted** function. This function takes the *List* as a parameter and returns |
|
345 a sorted copy of the list. However the original list is left intact:: |
|
346 |
|
347 >>> a = [5, 1, 3, 7, 4] |
|
348 >>> b = sorted(a) |
|
349 >>> b |
|
350 [1, 3, 4, 5, 7] |
|
351 >>> a |
|
352 [5, 1, 3, 7, 4] |
|
353 |