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-- |
114 | 1 |
* Lists |
2 |
*** Outline |
|
3 |
***** Lists |
|
4 |
***** Tuples |
|
5 |
***** Arsenal Required |
|
6 |
*** Script |
|
7 |
Welcome friends. |
|
8 |
||
116 | 9 |
In this tutorial we shall look at some special Data structures |
10 |
supported by Python namely Lists and Tuples. We have already been |
|
11 |
introduced to lists in some of the previous tutorials, here we |
|
12 |
shall look at them in little more detail. |
|
114 | 13 |
|
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 | 17 |
|
18 |
First lets start the interpreter by typing ipython in terminal. |
|
19 |
We create our first list by typing |
|
20 |
num = [1, 2, 3, 4] |
|
21 |
Items enclosed in square brackets separated by comma |
|
22 |
constitutes a list. |
|
116 | 23 |
One neat feature of Python list is that we can store data of any |
24 |
type in them. We can have a list something like: |
|
114 | 25 |
var = [1, 1.2, 'string'] |
26 |
print var |
|
27 |
||
28 |
Similar to strings, we can concatenate two lists using '+' |
|
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 | 38 |
To add single object at the end of a list the 'append' |
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 | 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 | 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 | 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 | 53 |
num = [1, 4, -6] |
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 | 61 |
num.reverse() |
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 | 65 |
To remove a particular element from the list Python provides |
66 |
the remove() function |
|
67 |
num.remove(8) |
|
68 |
if the given argument is present more than once in the list, |
|
69 |
then the first occurrence of that element is removed from list. |
|
114 | 70 |
|
116 | 71 |
The Slicing and Striding concepts which we covered for Arrays work |
72 |
with lists as well. Lets revisit the concept by looking at some examples |
|
114 | 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 | 75 |
One important feature of list indexing is the negative index. In |
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 | 78 |
similarly -2 will be second last and so forth. Now these |
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 | 81 |
we get list which excludes first and last element of a. |
116 | 82 |
and if we do not specify the start or the end index value the default |
83 |
values are taken. The default values being the first element and the |
|
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 | 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 | 88 |
This gives second, fourth and so on items of a till we reach |
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 | 91 |
With step sizes, if we specify negative values we get some |
114 | 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 | 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 | 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 | 108 |
True |
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 | 111 |
Python provides support for special immutable lists known as |
112 |
'tuple' To create a tuple instead we use normal brackets '(' |
|
113 |
unlike '[' for lists. |
|
114 | 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 | 117 |
but operation like |
118 |
t[4] = 7 are not allowed |
|
119 |
These features of tuples have their advantages. To see where |
|
120 |
are they used we first create two variables |
|
121 |
a, b = 1, 6 |
|
122 |
print a, b |
|
116 | 123 |
As you can see multiple variable assignments are possible using |
124 |
tuples. |
|
114 | 125 |
Now lets swap values their values. Normal approach would be |
126 |
to create a temporary to hold the value but because of tuples |
|
127 |
we can do something cool like |
|
128 |
b, a = a, b |
|
129 |
print a, b |
|
130 |
and values are swapped. And this swapping works for all types |
|
116 | 131 |
of variables. This is possible because of something magical |
132 |
that Python does called as tuple packing and unpacking. |
|
114 | 133 |
|
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 | 140 |
|
141 |
*** Notes |