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