lists.org
author Shantanu <shantanu@fossee.in>
Wed, 28 Apr 2010 16:33:18 +0530
changeset 115 d35eccbf206d
parent 114 751cb19549c8
child 116 8b650688f4e1
permissions -rw-r--r--
Added dictionary.org file.
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
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
     9
	In this tutorial we shall look at Data structures supported by
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    10
	Python.	We shall look at Lists and Tuples. We have already seen
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    11
	lists in some of previous sessions, here we shall cover more 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    12
	details about them.
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 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    15
	objects, in a given order. The list type implements the sequence
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.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    24
	One neat feature of Python list is we can store data belonging
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    25
	to multiple kind of data structures. We can have a list with 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    26
	something:
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    27
	var = [1, 1.2, 'string']
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    28
	print var
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    29
	and with this list we can perform most of list operations.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    30
	Python lists are very versatile, that is we can change it as we 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    31
	wish. It supports features like removal, addition. 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    32
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    33
	Similar to strings, we can concatenate two lists using '+' 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    34
	operator
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    35
	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
    36
	'num'
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    37
	We have already covered append functionality.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    38
	To add single object at end of list 'append' function is used
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
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    53
	As we can notice extend and append behaves differently.
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
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    60
	To remove a particular element from list Python provides remove
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    61
	num.reverse(8)
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    62
	if argument is present more than once, then first occurrence is
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    63
	removed from list.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    64
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    65
	Slicing and Striding concepts which we covered for Arrays work
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    66
	with lists also. Lets revisit the concept with some examples
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    67
	a = [1, 2, 3, 4, 5]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    68
	a[1:3] returns a list with second and third element of a
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    69
	One important feature of list indexing is negative values. In
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    70
	Lists -1 indicates last element of a list
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    71
	a[-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    72
	similarly -2 will be second last and so forth. Now these 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    73
	negative indexes can also be used with slicing. If we try
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    74
	a[1:-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    75
	we get list which excludes first and last element of a.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    76
	and if we miss start/end index value default are taken
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    77
	a[:3] will return list from start till fourth element of a.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    78
	If we give step size we can do striding also
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    79
	a[1:-1:2]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    80
	This gives second, fourth and so on items of a till we reach 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    81
	last item of list.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    82
	a[::2] will skip all the even placed elements of a
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    83
	With step sizes, if we give negative values we get some 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    84
	interesting results. Lets try
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    85
	a[::-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    86
	It returns reversed 'a'
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    87
	We can check for containership also with lists
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    88
	num
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    89
	4 in a
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    90
	True
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    91
	
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    92
	Python provides support to special immutable lists known as
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    93
	'tuple'	To create a tuple instead of square brackets'[' we have
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    94
	to use normal brackets '('
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    95
	t = (1, 2, 3, 4, 5, 6, 7, 8)
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    96
	its elements can also be accessed using indexes
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    97
	t[0] + t[3] + t[-1]
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    98
	but operation like
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
    99
	t[4] = 7 are not allowed
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   100
	These features of tuples have their advantages. To see where 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   101
	are they used we first create two variables
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   102
	a, b = 1, 6
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   103
	print a, b
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   104
	Now lets swap values their values. Normal approach would be 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   105
	to create a temporary to hold the value but because of tuples
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   106
	we can do something cool like
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   107
	b, a = a, b
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   108
	print a, b
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   109
	and values are swapped. And this swapping works for all types
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   110
	of variables.
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   111
	
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   112
	With this we come to the end of this tutorial on Lists and 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   113
	tuples. In this tutorial we have learnt some more operations 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   114
	on lists and tuples. In next session we will cover more on 
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   115
	Python supported data structures. Thank you!
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   116
751cb19549c8 Added lists.org.
Shantanu <shantanu@fossee.in>
parents:
diff changeset
   117
*** Notes