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