diff -r 2f247bcfae8b -r 7dc53e6c8065 lists.org --- a/lists.org Tue May 04 16:52:38 2010 +0530 +++ b/lists.org Tue May 04 17:21:12 2010 +0530 @@ -12,9 +12,8 @@ shall look at them in little more detail. The list type is a container that holds a number of other - objects, in the given order. The list type implements the sequence - protocol, and also allows you to add and remove objects from - the sequence. + objects, in the given order. Lists allow you to add and + remove objects from the sequence. First lets start the interpreter by typing ipython in terminal. We create our first list by typing @@ -25,39 +24,44 @@ type in them. We can have a list something like: var = [1, 1.2, 'string'] print var - and with this list we can perform most of list operations. - Python lists are very versatile, that is we can change it as we - wish. It supports features like removal, addition, sort, etc. Similar to strings, we can concatenate two lists using '+' operator - so num + var will return a new list with 'var' added in end of - 'num' - We have already covered the append function. + so num + var will return a new list with the contents of both + 'num' and 'var' one after the other. + Let's look at what num contains now + print num + As you can see num is unchanged by the '+' operator. + + We have already covered the append function in one of our previous + tutorials. To add single object at the end of a list the 'append' function is used - num + Let's now append -5 to it. num.append(-5) - num + The contents of num have been changed now. + print num append takes only one argument. And append behaves different - from + operator. While + will return new list with two lists - added if we try similar with append function like: + from + operator. While + returns a new list with two lists + added, append will simply add the entire object to the + end of the list: num.append([9, 10, 11]) - num - It changes original list and add the argument as one element - and not separate elements. - To extend list with new list elements we use 'extend' function + print num + It adds the entire list as one element and not separate elements. + In order to add separate elements we use the 'extend' function + Let's reinitialize num num = [1, 4, -6] num.extend([2, 8, 0]) - num - As we can notice extend and append behave differently. - To reverse a list 'reverse' function is available. - num - This is current content of list + print num + + Let's now move on to see more functions available + with lists. + To reverse a list, we have the 'reverse' function. + Please note the order of the elements in num. Let's now do: num.reverse() Now after using reverse function, lets check the value of 'num' - num - Please note, reverse actually manipulated the list. + print num + Please note, the reverse() function actually manipulated the list. To remove a particular element from the list Python provides the remove() function num.remove(8) @@ -67,40 +71,49 @@ The Slicing and Striding concepts which we covered for Arrays work with lists as well. Lets revisit the concept by looking at some examples a = [1, 2, 3, 4, 5] - a[1:3] returns a list with second and third element of 'a' + print a[1:3] returns a list with second and third element of 'a' One important feature of list indexing is the negative index. In Lists -1 indicates last element of the list - a[-1] + print a[-1] similarly -2 will be second last and so forth. Now these negative indexes can also be used with slicing. If we try - a[1:-1] + print a[1:-1] we get list which excludes first and last element of a. and if we do not specify the start or the end index value the default values are taken. The default values being the first element and the last element. - a[:3] will return a list from beginning upto the fourth element of a. + print a[:3] will return a list from beginning upto the fourth element of a. We can perform striding as well, by specifying the step size - a[1:-1:2] + print a[1:-1:2] This gives second, fourth and so on items of a till we reach last item of list. - a[::2] will skip all the even placed elements of a + print a[::2] will skip all the even placed elements of a With step sizes, if we specify negative values we get some interesting results. Lets try - a[::-1] - It returns reversed 'a' - We can check for containership with lists as well. + print a[4:1:-1] + Here we begin at the 5th element and go upto the 2nd element in the + reverse order since step size is -1 + print a[::-1] + This returns a slice with all the elements in 'a' reversed in order. + Here the negative step indicates that the start point has to be the + last element and the end point has to be the first element and the order + has to be reversed. + + Let's now move on to other functionality + We can check for containership of elements within lists as well. Let's look at the contents of num - num + print num To check if the number 4 is present in the list we type - 4 in a + 4 in num True + Now let's move onto Tuples. Python provides support for special immutable lists known as 'tuple' To create a tuple instead we use normal brackets '(' unlike '[' for lists. t = (1, 2, 3, 4, 5, 6, 7, 8) - its elements can also be accessed using indexes - t[0] + t[3] + t[-1] + its elements can also be accessed using indexes just like lists. + print t[0] + t[3] + t[-1] but operation like t[4] = 7 are not allowed These features of tuples have their advantages. To see where @@ -119,8 +132,10 @@ that Python does called as tuple packing and unpacking. With this we come to the end of this tutorial on Lists and - tuples. In this tutorial we have learnt some more operations - on lists and tuples. In next session we will cover more on - Python supported data structures. Thank you! + tuples. In this tutorial we have learnt about initializing, + various list operations, slicing and striding. We learnt + about tuple initialization, packing and unpacking. In next + session we will cover more on Python supported data + structures. Thank you! *** Notes