diff -r 000a414fc3b7 -r dbc118349011 basic_python/list_tuples.rst --- a/basic_python/list_tuples.rst Thu Aug 20 04:21:45 2009 +0530 +++ b/basic_python/list_tuples.rst Thu Aug 20 19:53:56 2009 +0530 @@ -177,7 +177,7 @@ None, Empty Lists, and Initialization -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +------------------------------------- An *Empty List* is a *List* with no elements and is simply represented as []. A *None List* is one with all elements in it being **None**. It serves @@ -191,3 +191,163 @@ >>> n [None, None, None, None, None, None, None, None, None, None] + +Nested Lists +------------ + +As mentioned earlier, a List can contain elements of any data type. This also +implies a *List* can have a *Lists* themselves as its elements. These are +called as *Nested Lists*. There is no limit on the depth of the *Nested Lists*:: + + >>> a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7] + + +List Methods +------------ + +A method is a function that is coupled to an object. More about objects +and its methods are discussed in Advanced Python module. In general, a +method is called like:: + + object.method(arguments) + +For now, it is enough to know that a list of elements is an object and +so *List* methods can be called upon them. Also some of the methods change +the *List* in-place, meaning it modifies the existing list instead of creating +a new one, while other methods don't. It must be noted as we run through +the *List* methods. + +Some of the most commonly used *List* methods are as follows: + + +append +~~~~~~ + +The *append* method is used to append an object at the end of the list:: + + >>> prime = [2, 3, 5] + >>> prime.append(7) + >>> prime + [2, 3, 5, 7] + +It is important to note that append changes the *List* in-place. + + +count +~~~~~ + +The *count* method returns the number of occurences of a particular element +in a list:: + + >>> [1, 4, 4, 9, 9, 9].count(9) + 3 + >>> tlst = ['Python', 'is', 'a', 'beautiful', 'language'] + >>> tlst.count('Python') + 1 + + +extend +~~~~~~ + +The *extend* method extends the list on which it is called by the list supplied +as argument to it:: + + >>> a = [1, 2, 3] + >>> b = [4, 5, 6] + >>> a.extend(b) + [1, 2, 3, 4, 5, 6] + +This is an in-place method. This method is equivalent to using the + operator, but +using the + operator returns a new list. + + +index +~~~~~ + +The *index* method returns the index position of the element in the list +specified as argument:: + + >>> a = [1, 2, 3, ,4, 5] + >>> a.index(4) + 3 + + +insert +~~~~~~ + +The *insert* method is used to insert an element specified as the second +argument to the list at the position specified by the first argument:: + + >>> a = ['Python', 'is', 'cool'] + >>> a.insert(2, 'so') + >>> a + ['Python', 'is', 'so', 'cool'] + +The *insert* method changes the *List* in-place. + + +pop +~~~ + +The *pop* method removes an element from the list. The index position +of the element to be removed can be specified as an argument to the +*pop* method, if not it removes the last element by default:: + + >>> a = [1, 2, 3, 4, 5] + >>> a.pop() + >>> a + 5 + >>> a.pop(2) + >>> a + 3 + +The *pop* method changes the *List* in-place. + + +remove +~~~~~~ + +The *remove* method removes the first occurence of an element supplied as a +parameter:: + + >>> a = [1, 2, 3, 4, 2, 5, 2] + >>> a.remove(2) + >>> a + [1, 3, 4, 2, 5, 2] + + +reverse +~~~~~~~ + +The *reverse* method reverses elements in the list. It is important to note +here that *reverse* method changes the list in-place and doesn't return any +thing:: + + >>> a = ['guido', 'alex', 'tim'] + >>> a.reverse() + >>> a + ['tim', 'alex', 'guido'] + + +sort +~~~~ + +The *sort* method is used to sort the elements of the list. The *sort* method +also sorts in-place and does not return anything:: + + >>> a = [5, 1, 3, 7, 4] + >>> a.sort() + >>> a + [1, 3, 4, 5, 7] + +In addition to the sort method on a *List* object we can also use the built-in +**sorted** function. This function takes the *List* as a parameter and returns +a sorted copy of the list. However the original list is left intact:: + + >>> a = [5, 1, 3, 7, 4] + >>> b = sorted(a) + >>> b + [1, 3, 4, 5, 7] + >>> a + [5, 1, 3, 7, 4] +