author | Madhusudan.C.S <madhusudancs@gmail.com> |
Thu, 20 Aug 2009 20:26:21 +0530 | |
changeset 6 | 6921d82a80db |
parent 5 | dbc118349011 |
child 11 | edd18b1f5cb8 |
permissions | -rw-r--r-- |
4
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
1 |
Lists and Tuples |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
2 |
================ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
3 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
4 |
Python provides an intuitive way to represent a group items, called *Lists*. The |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
5 |
items of a *List* are called its elements. Unlike C/C++, elements can be of any |
6
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
6 |
type. A *List* is represented as a list of comma-sepated elements with square |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
7 |
brackets around them:: |
4
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
8 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
9 |
>>> a = [10, 'Python programming', 20.3523, 23, 3534534L] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
10 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
11 |
[10, 'Python programming', 20.3523, 23, 3534534L] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
12 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
13 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
14 |
Common List Operations |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
15 |
---------------------- |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
16 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
17 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
18 |
Indexing |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
19 |
~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
20 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
21 |
Individual elements of a *List* can be accessed using an index to the element. |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
22 |
The indices start at 0. One can also access the elements of the *List* in reverse |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
23 |
using negative indices.:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
24 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
25 |
>>> a[1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
26 |
'Python programming' |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
27 |
>>> a[-1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
28 |
3534534L |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
29 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
30 |
It is important to note here that the last element of the *List* has an index of |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
31 |
-1. |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
32 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
33 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
34 |
Concatenating |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
35 |
~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
36 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
37 |
Two or more *Lists* can be concatenated using the + operator:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
38 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
39 |
>>> a + ['foo', 12, 23.3432, 54] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
40 |
[10, 'Python programming', 20.3523, 'foo', 12, 23.3432, 54] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
41 |
>>> [54, 75, 23] + ['write', 67, 'read'] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
42 |
[54, 75, 23, 'write', 67, 'read'] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
43 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
44 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
45 |
Slicing |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
46 |
~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
47 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
48 |
A *List* can be sliced off to contain a subset of elements of the *List*. Slicing |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
49 |
can be done by using two indices separated by a colon, where the first index is |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
50 |
inclusive and the second index is exclusive. The resulting slice is also a *List*.:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
51 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
52 |
>>> num = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
53 |
>>> num[3:6] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
54 |
[4, 5, 6] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
55 |
>>> num[0:1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
56 |
[1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
57 |
>>> num[7:10] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
58 |
[7, 8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
59 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
60 |
The last example showed how to access last 3 elements of the *List*. There is a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
61 |
small catch here. The second index 10 actually refers to the 11th element of the |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
62 |
*List* which is still valid, even though it doesn't exist because the second |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
63 |
index is exclusive and tells the Python interpreter to get the last element of |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
64 |
the *List*. But this can also be done in a much easier way using negative indices:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
65 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
66 |
>>> num[-3:-1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
67 |
[7, 8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
68 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
69 |
Excluding the first index implies that the slice must start at the beginning of |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
70 |
the *List*, while excluding the second index includes all the elements till the |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
71 |
end of the *List*. A third parameter to a slice, which is implicitly taken as 1 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
72 |
is the step of the slice. It is specified as a value which follows a colon after |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
73 |
the second index:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
74 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
75 |
>>> num[:4] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
76 |
[1, 2, 3, 4] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
77 |
>>> num[7:] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
78 |
[8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
79 |
>>> num[-3:] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
80 |
[7, 8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
81 |
>>> num[:] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
82 |
[1, 2, 3, 4, 5, 6, 7, 8, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
83 |
>>> num[4:9:3] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
84 |
[5, 8] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
85 |
>>> num[3::2] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
86 |
[4, 6, 8] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
87 |
>>> num[::4] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
88 |
[1, 5, 9] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
89 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
90 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
91 |
Multiplication |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
92 |
~~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
93 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
94 |
A *List* can be multiplied with an integer to repeat itself:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
95 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
96 |
>>> [20] * 5 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
97 |
[20, 20, 20, 20, 20] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
98 |
>>> [42, 'Python', 54] * 3 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
99 |
[42, 'Python', 54, 42, 'Python', 54, 42, 'Python', 54] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
100 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
101 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
102 |
Membership |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
103 |
~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
104 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
105 |
**in** operator is used to find whether an element is part of the *List*. It |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
106 |
returns **True** if the element is present in the *List* or **False** if it is not |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
107 |
present. Since this operator returns a Boolean value it is called a Boolean |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
108 |
operator:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
109 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
110 |
>>> names = ['Guido', 'Alex', 'Tim'] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
111 |
>>> 'Tim' in names |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
112 |
True |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
113 |
>>> 'Adam' in names |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
114 |
False |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
115 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
116 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
117 |
Length, Maximum and Minimum |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
118 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
119 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
120 |
Length of a *List* can be found out using the len function. The max function |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
121 |
returns the element with the largest value and the min function returns the |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
122 |
element with the smallest value:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
123 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
124 |
>>> num = [4, 1, 32, 12, 67, 34, 65] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
125 |
>>> len(num) |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
126 |
7 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
127 |
>>> max(num) |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
128 |
67 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
129 |
>>> min(num) |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
130 |
1 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
131 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
132 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
133 |
Changing Elements |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
134 |
~~~~~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
135 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
136 |
Unlike Strings *Lists* are mutable, i.e. elements of a *List* can be manipulated:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
137 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
138 |
>>> a = [1, 3, 5, 7] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
139 |
>>> a[2] = 9 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
140 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
141 |
[1, 3, 9, 7] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
142 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
143 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
144 |
Deleting Elements |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
145 |
~~~~~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
146 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
147 |
An element or a slice of a *List* can be deleted by using the **del** statement:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
148 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
149 |
>>> a = [1, 3, 5, 7, 9, 11] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
150 |
>>> del a[-2:] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
151 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
152 |
[1, 3, 5, 7] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
153 |
>>> del a[1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
154 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
155 |
[1, 5, 7] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
156 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
157 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
158 |
Assign to Slices |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
159 |
~~~~~~~~~~~~~~~~ |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
160 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
161 |
In the same way, values can be assigned to individual elements of the *List*, |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
162 |
a *List* of elements can be assigned to a slice:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
163 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
164 |
>>> a = [2, 3, 4, 5] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
165 |
>>> a[:2] = [0, 1] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
166 |
[0, 1, 4, 5] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
167 |
>>> a[2:2] = [2, 3] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
168 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
169 |
[0, 1, 2, 3, 4, 5] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
170 |
>>> a[2:4] = [] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
171 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
172 |
[0, 1, 4, 5] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
173 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
174 |
The last two examples should be particularly noted carefully. The last but one |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
175 |
example insert elements or a list of elements into a *List* and the last example |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
176 |
deletes a list of elements from the *List*. |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
177 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
178 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
179 |
None, Empty Lists, and Initialization |
5
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
180 |
------------------------------------- |
4
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
181 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
182 |
An *Empty List* is a *List* with no elements and is simply represented as |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
183 |
[]. A *None List* is one with all elements in it being **None**. It serves |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
184 |
the purpose having a container list of some fixed number of elements with |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
185 |
no value:: |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
186 |
|
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
187 |
>>> a = [] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
188 |
>>> a |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
189 |
[] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
190 |
>>> n = [None] * 10 |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
191 |
>>> n |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
192 |
[None, None, None, None, None, None, None, None, None, None] |
000a414fc3b7
Added a file for Lists and Tuples session.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
193 |
|
5
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
194 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
195 |
Nested Lists |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
196 |
------------ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
197 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
198 |
As mentioned earlier, a List can contain elements of any data type. This also |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
199 |
implies a *List* can have a *Lists* themselves as its elements. These are |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
200 |
called as *Nested Lists*. There is no limit on the depth of the *Nested Lists*:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
201 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
202 |
>>> a = [1, [1, 2, 3], 3, [1, [1, 2, 3]], 7] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
203 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
204 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
205 |
List Methods |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
206 |
------------ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
207 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
208 |
A method is a function that is coupled to an object. More about objects |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
209 |
and its methods are discussed in Advanced Python module. In general, a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
210 |
method is called like:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
211 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
212 |
object.method(arguments) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
213 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
214 |
For now, it is enough to know that a list of elements is an object and |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
215 |
so *List* methods can be called upon them. Also some of the methods change |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
216 |
the *List* in-place, meaning it modifies the existing list instead of creating |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
217 |
a new one, while other methods don't. It must be noted as we run through |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
218 |
the *List* methods. |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
219 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
220 |
Some of the most commonly used *List* methods are as follows: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
221 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
222 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
223 |
append |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
224 |
~~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
225 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
226 |
The *append* method is used to append an object at the end of the list:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
227 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
228 |
>>> prime = [2, 3, 5] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
229 |
>>> prime.append(7) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
230 |
>>> prime |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
231 |
[2, 3, 5, 7] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
232 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
233 |
It is important to note that append changes the *List* in-place. |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
234 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
235 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
236 |
count |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
237 |
~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
238 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
239 |
The *count* method returns the number of occurences of a particular element |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
240 |
in a list:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
241 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
242 |
>>> [1, 4, 4, 9, 9, 9].count(9) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
243 |
3 |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
244 |
>>> tlst = ['Python', 'is', 'a', 'beautiful', 'language'] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
245 |
>>> tlst.count('Python') |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
246 |
1 |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
247 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
248 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
249 |
extend |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
250 |
~~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
251 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
252 |
The *extend* method extends the list on which it is called by the list supplied |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
253 |
as argument to it:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
254 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
255 |
>>> a = [1, 2, 3] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
256 |
>>> b = [4, 5, 6] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
257 |
>>> a.extend(b) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
258 |
[1, 2, 3, 4, 5, 6] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
259 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
260 |
This is an in-place method. This method is equivalent to using the + operator, but |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
261 |
using the + operator returns a new list. |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
262 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
263 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
264 |
index |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
265 |
~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
266 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
267 |
The *index* method returns the index position of the element in the list |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
268 |
specified as argument:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
269 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
270 |
>>> a = [1, 2, 3, ,4, 5] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
271 |
>>> a.index(4) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
272 |
3 |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
273 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
274 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
275 |
insert |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
276 |
~~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
277 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
278 |
The *insert* method is used to insert an element specified as the second |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
279 |
argument to the list at the position specified by the first argument:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
280 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
281 |
>>> a = ['Python', 'is', 'cool'] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
282 |
>>> a.insert(2, 'so') |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
283 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
284 |
['Python', 'is', 'so', 'cool'] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
285 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
286 |
The *insert* method changes the *List* in-place. |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
287 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
288 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
289 |
pop |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
290 |
~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
291 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
292 |
The *pop* method removes an element from the list. The index position |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
293 |
of the element to be removed can be specified as an argument to the |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
294 |
*pop* method, if not it removes the last element by default:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
295 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
296 |
>>> a = [1, 2, 3, 4, 5] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
297 |
>>> a.pop() |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
298 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
299 |
5 |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
300 |
>>> a.pop(2) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
301 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
302 |
3 |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
303 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
304 |
The *pop* method changes the *List* in-place. |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
305 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
306 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
307 |
remove |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
308 |
~~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
309 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
310 |
The *remove* method removes the first occurence of an element supplied as a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
311 |
parameter:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
312 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
313 |
>>> a = [1, 2, 3, 4, 2, 5, 2] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
314 |
>>> a.remove(2) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
315 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
316 |
[1, 3, 4, 2, 5, 2] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
317 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
318 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
319 |
reverse |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
320 |
~~~~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
321 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
322 |
The *reverse* method reverses elements in the list. It is important to note |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
323 |
here that *reverse* method changes the list in-place and doesn't return any |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
324 |
thing:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
325 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
326 |
>>> a = ['guido', 'alex', 'tim'] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
327 |
>>> a.reverse() |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
328 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
329 |
['tim', 'alex', 'guido'] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
330 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
331 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
332 |
sort |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
333 |
~~~~ |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
334 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
335 |
The *sort* method is used to sort the elements of the list. The *sort* method |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
336 |
also sorts in-place and does not return anything:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
337 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
338 |
>>> a = [5, 1, 3, 7, 4] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
339 |
>>> a.sort() |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
340 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
341 |
[1, 3, 4, 5, 7] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
342 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
343 |
In addition to the sort method on a *List* object we can also use the built-in |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
344 |
**sorted** function. This function takes the *List* as a parameter and returns |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
345 |
a sorted copy of the list. However the original list is left intact:: |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
346 |
|
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
347 |
>>> a = [5, 1, 3, 7, 4] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
348 |
>>> b = sorted(a) |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
349 |
>>> b |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
350 |
[1, 3, 4, 5, 7] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
351 |
>>> a |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
352 |
[5, 1, 3, 7, 4] |
dbc118349011
Added List methods to Lists and Tuples section.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
4
diff
changeset
|
353 |
|
6
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
354 |
|
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
355 |
List Comprehensions |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
356 |
------------------- |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
357 |
|
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
358 |
List Comprehension is a convenvience utility provided by Python. It is a |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
359 |
syntatic sugar to create *Lists*. Using *List Comprehensions* one can create |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
360 |
*Lists* from other type of sequential data structures or other *Lists* itself. |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
361 |
The syntax of *List Comprehensions* consists of a square brackets to indicate |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
362 |
the result is a *List* within which we include at least one **for** clause and |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
363 |
multiple **if** clauses. It will be more clear with an example:: |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
364 |
|
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
365 |
>>> num = [1, 2, 3] |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
366 |
>>> sq = [x*x for x in num] |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
367 |
>>> sq |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
368 |
[1, 4, 9] |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
369 |
>>> all_num = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
370 |
>>> even = [x for x in all_num if x%2 == 0] |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
371 |
|
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
372 |
The syntax used here is very clear from the way it is written. It can be |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
373 |
translated into english as, "for each element x in the list all_num, |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
374 |
if remainder of x divided by 2 is 0, add x to the list." |
6921d82a80db
Added a subsection on List Comprehension.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
5
diff
changeset
|
375 |