MODULE III
3.1 LISTS
A list is a sequence, Lists are mutable, Traversing a list, List operations, List slices, List Methods,
Deleting elements, Lists and functions, Lists and strings, Parsing lines, Objects and values , Aliasing,
List arguments, Debugging
3.2 DICTIONARIES
Introduction, Dictionary as a set of counters, Dictionaries and files, Looping and Advanced text
parsing, Debugging
3.3 TUPLES
Tuples are immutable, Comparing tuples, Tuple assignment Dictionaries and tuples, Multiple
assignment with dictionaries, The most common words, Using tuples as keys in dictionaries, Sequences:
strings, lists, and tuples, Debugging
3.4 REGULAR EXPRESSIONS
Character matching in regular expressions, Extracting data using regular expressions, Combining searching
and extracting Escape character, Summary, Bonus section for Unix / Linux users
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 1
, Python Application Programming (15CS664) Module III
MODULE III
3.1 LISTS
A list is an ordered sequence of values.
It is a data structure in Python. The values inside the lists can be of any type (like integer, float,
strings, lists, tuples, dictionaries etc) and are called as elements or items.
The elements of lists are enclosed within square brackets.
For example,
ls1=[10,-4, 25, 13]
ls2=[“Tiger”, “Lion”, “Cheetah”]
Here, ls1 is a list containing four integers, and ls2 is a list containing three strings.
A list need not contain data of same type.
We can have mixed type of elements in list.
For example,
ls3=[3.5, „Tiger‟, 10, [3,4]]
Here, ls3 contains a float, a string, an integer and a list.
This illustrates that a list can be nested as well.
An empty list can be created any of the following ways –
>>> ls =[]
>>> type(ls)
<class 'list'>
or
>>> ls =list()
>>> type(ls)
<class 'list'>
In fact, list() is the name of a method (special type of method called as constructor – which will be
discussed in Module 4) of the class list.
Hence, a new list can be created using this function by passing arguments to it as shown below –
>>> ls2=list([3,4,1])
>>> print(ls2)
[3, 4, 1]
Lists are Mutable
The elements in the list can be accessed using a numeric index within square-brackets.
It is similar to extracting characters in a string.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[1])
hi
>>> print(ls[2])
[2, 3]
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 2
,Python Application Programming (15CS664) Module III
Observe here that, the inner list is treated as a single element by outer list. If we would like to
access the elements within inner list, we need to use double-indexing as shown below –
>>> print(ls[2][0]) 2
>>> print(ls[2][1]) 3
Note that, the indexing for inner-list again starts from 0.
Thus, when we are using double- indexing, the first index indicates position of inner list inside
outer list, and the second index means the position particular value within inner list.
Unlike strings, lists are mutable. That is, using indexing, we can modify any value within list.
In the following example, the 3rd element (i.e. index is 2) is being modified –
>>> ls=[34, 'hi', [2,3],-5]
>>> ls[2]='Hello'
>>> print(ls)
[34, 'hi', 'Hello', -5]
The list can be thought of as a relationship between indices and elements. This relationship is
called as a mapping. That is, each index maps to one of the elements in a list.
The index for extracting list elements has following properties –
Any integer expression can be an index.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[2*1])
[2,3]
Attempt to access a non-existing index will throw and IndexError.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[4])
IndexError: list index out of range
A negative indexing counts from backwards.
>>> ls=[34, 'hi', [2,3],-5]
>>> print(ls[-1])
-5
>>> print(ls[-3])
hi
The in operator applied on lists will results in a Boolean value.
>>> ls=[34, 'hi', [2,3],-5]
>>> 34 in ls
True
>>> -2 in ls
False
Traversing a List
A list can be traversed using for loop.
If we need to use each element in the list, we can use the for loop and in operator as below
>>> ls=[34, 'hi', [2,3],-5]
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 3
, Python Application Programming (15CS664) Module III
>>> for item in ls:
print(item)
34
hi
[2,3]
-5
List elements can be accessed with the combination of range() and len() functions as well –
ls=[1,2,3,4]
for i in range(len(ls)):
ls[i]=ls[i]**2
print(ls)
#output is
[1, 4, 9, 16]
Here, we wanted to do modification in the elements of list. Hence, referring indices is suitable
than referring elements directly.
The len() returns total number of elements in the list (here it is 4).
Then range() function makes the loop to range from 0 to 3 (i.e. 4-1).
Then, for every index, we are updating the list elements (replacing original value by its square).
List Operations
Python allows to use operators + and * on lists.
The operator + uses two list objects and returns concatenation of those two lists.
Whereas * operator take one list object and one integer value, say n, and returns a list by repeating
itself for n times.
>>> ls1=[1,2,3]
>>> ls2=[5,6,7]
>>> print(ls1+ls2) #concatenation using +
[1, 2, 3, 5, 6, 7]
>>> ls1=[1,2,3]
>>> print(ls1*3) #repetition using *
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> [0]*4 #repetition using *
[0, 0, 0, 0]
List Slices
Similar to strings, the slicing can be applied on lists as well. Consider a list t given below, and a
series of examples following based on this object.
Mamatha A, Asst Prof, Dept of CSE, SVIT Page 4