lists.org
author Santosh G. Vattam <vattam.santosh@gmail.com>
Thu, 29 Apr 2010 17:59:25 +0530
changeset 116 8b650688f4e1
parent 114 751cb19549c8
child 119 7dc53e6c8065
permissions -rw-r--r--
Minor edits.
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 
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    15
	objects, in the given order. The list type implements the sequence
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    16
	protocol, and also allows you to add and remove objects from 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    17
	the sequence. 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    18
	
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    19
	First lets start the interpreter by typing ipython in terminal.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    20
	We create our first list by typing 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    21
	num = [1, 2, 3, 4]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    22
	Items enclosed in square brackets separated by comma 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    23
	constitutes a list.
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    24
	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
    25
	type in them. We can have a list something like:
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
	var = [1, 1.2, 'string']
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
	print var
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
	and with this list we can perform most of list operations.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
	Python lists are very versatile, that is we can change it as we 
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    30
	wish. It supports features like removal, addition, sort, etc. 
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
	Similar to strings, we can concatenate two lists using '+' 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
	operator
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
	so num + var will return a new list with 'var' added in end of
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
	'num'
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    36
	We have already covered the append function.
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    37
	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
    38
	function is used
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    39
	num
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    40
	num.append(-5)
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    41
	num
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    42
	append takes only one argument. And append behaves different 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    43
	from + operator. While + will return new list with two lists 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    44
	added if we try similar with append function like:
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    45
	num.append([9, 10, 11])
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    46
	num
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    47
	It changes original list and add the argument as one element
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    48
	and not separate elements.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    49
	To extend list with new list elements we use 'extend' function
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    50
	num = [1, 4, -6]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    51
	num.extend([2, 8, 0])
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    52
	num
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    53
	As we can notice extend and append behave differently.
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    54
	To reverse a list 'reverse' function is available. 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    55
	num
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    56
	This is current content of list
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    57
	num.reverse()
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    58
	Now after using reverse function, lets check the value of 'num'
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    59
	num
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    60
	Please note, reverse actually manipulated the list. 
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    61
	To remove a particular element from the list Python provides
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    62
	the remove() function
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    63
	num.remove(8)
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    64
	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
    65
	then the first occurrence of that element is removed from list.
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    67
	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
    68
	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
    69
	a = [1, 2, 3, 4, 5]
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    70
	a[1:3] returns a list with second and third element of 'a'
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    71
	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
    72
	Lists -1 indicates last element of the list
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
	a[-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
	similarly -2 will be second last and so forth. Now these 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
	negative indexes can also be used with slicing. If we try
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
	a[1:-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
	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
    78
	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
    79
	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
    80
	last element.
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    81
	a[:3] will return a list from beginning upto the fourth element of a.
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    82
	We can perform striding as well, by specifying the step size
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
	a[1:-1:2]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
	This gives second, fourth and so on items of a till we reach 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
	last item of list.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
	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
    87
	With step sizes, if we specify negative values we get some 
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
	interesting results. Lets try
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
	a[::-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
	It returns reversed 'a'
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    91
	We can check for containership with lists as well.
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    92
	Let's look at the contents of num
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
	num
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    94
	To check if the number 4 is present in the list we type
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
	4 in a
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
	True
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
	
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    98
	Python provides support for special immutable lists known as
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
    99
	'tuple'	To create a tuple instead we use normal brackets '('
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
   100
	unlike '[' for lists.
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
	t = (1, 2, 3, 4, 5, 6, 7, 8)
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
	its elements can also be accessed using indexes
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
	t[0] + t[3] + t[-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
	but operation like
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
	t[4] = 7 are not allowed
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
	These features of tuples have their advantages. To see where 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
	are they used we first create two variables
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
	a, b = 1, 6
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
	print a, b
116
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
   110
	As you can see multiple variable assignments are possible using
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
   111
	tuples.
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
	Now lets swap values their values. Normal approach would be 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
	to create a temporary to hold the value but because of tuples
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
	we can do something cool like
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
	b, a = a, b
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
	print a, b
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
	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
   118
	of variables. This is possible because of something magical 
8b650688f4e1 Minor edits.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 114
diff changeset
   119
	that Python does called	as tuple packing and unpacking.
114
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   120
	
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   121
	With this we come to the end of this tutorial on Lists and 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   122
	tuples. In this tutorial we have learnt some more operations 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   123
	on lists and tuples. In next session we will cover more on 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   124
	Python supported data structures. Thank you!
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   125
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   126
*** Notes