LIST MANIPULATION
LIST MANIPULATION
CHECK POINT
1 Why are lists called mutable types?
Lists are mutable ie, their value can be changed by adding or
changing an element in it.For example
L=[1,2,3]
L[1]=4 # changed the element at index 1
L.append(5) # element 5 is added to the list
Hence they are called mutable types in python.
2 What are immutable counterparts of lists?
Immutable counterpart of lists are tuples because their values can’t
be changed.For example
T=(1,2,3)
T[1] =4 #this will raise error
3 What are the different ways of creating lists?
*)L=list( <sequence>)
*)We can initialize the list with values like
L=[ ]
L=[1,2,3]
*) L=eval(input("Enter list elements"))
4 What values can we have in a list? Do they all have to be the same
type?
In lists we can store values of any data type.
No
5 How are individual elements of lists are accessed and changed?
Individual elements of lists are accessed and changed as
L[index]= new value
Eg:
L=[1,2,3]
L[2]=5
print(L)
[1,2,5]
6 How do you create the following lists?
a)[4,5,6]
L=[4,5,6] or L=list([4,5,6])
b)[-2,1,3]
L=[-2,1,3] or L= list([-2,1,3])
c)[-9,-8,-7,-6,-5]
L=[-9,-8,-7,-6,-5] or L =list([-9,-8,-7,-6,-5]) or L=list(range(-9,-4))
d)[-9,-10,-11,-12]
L=[-9,-10,-11,-12] or L =list([-9,-10,-11,-12]) or
L=list(range(-9,-,-1))
e)[0,1,2]
L=[0,1,2] or L =list([0,1,2]) or L=list(range(3))
7 If a =[5,4,3,2,1,0] evaluate the following expressions:
a)a[0]
b)a[-1]
c)a[a[0]]
d)a[a[-1]]
e)a[a[a[a[2] +1]]]
8 Can you change an element of a sequence? What if the sequence is a
string? What if the sequence is a list?
If the sequence type is mutable , then the element can be changed
otherwise not possible
If the sequence is a string , the element can’t be changed.
If the sequence is a list , the elements can be changed.
9 What does a+b amounts to if a and b are lists?
A new list with all the elements of ‘a’ and ‘b’ combined.
10 What does a*b amounts to if a and b are lists?
11 What does a+b amounts to if a is a list and b=5?
a+b will raise Type error
12 Is a string the same as a list of characters?
13 Which functions can you use to add elements to a list?
append( ) , extend( ) , insert( )
14 What is the difference between append() and insert() methods of list?
append( ) method inserts an element to the last of the list .
insert( ) method can insert an element at any position.
15 What is the difference between pop() and remove() methods of list?
pop( ) function delete and return the element of passed index. Index
passing is optional , if not passed , element from last will be deleted.
List.pop(<index>)
remove( ) function removes the first occurence of given item from
the list.
List.remove(<value>)
16 What is the difference between append() and extend() methods of
list?
append( ) function adds a single item to the end of the list.
List.append(<item>)
extend( ) function add multiple elements from a list supplied to it as
argument.
List.extend(<list>)
SOLVED PROBLEMS
1 How are lists different from strings when both are sequences?
2 What are nested lists?
3 What does each of the following expression evaluate to?
L = [“These”,”are”, “a” , [“few”,”words”],”that”,”we”,”will”,”use”]
a)L[3:4]
L[3:4][0]
L[3:4][0][1]
L[3:4][0][1][2]
b)”few” in L
c)”few” in L[3]
d)[L[1]] + L[3]
e)L[4:]
f)L[0: :2]
4 What does each of the following expression evaluate to?
L = [“These”,[”are”, “a” ], [“few”,”words”],”that”,”we”,”will”,”use”]
a)len(L)
b)L[3:4] +L[1:2]
c)”few” in L[2:3]
d)”few” in L[2:3][0]
e)”few” in L[2]
f)L[2][1:]
g)L[1] +L[2]
6 Write the most appropriate list method to perform the following
tasks.
a)Delete a given element from the list. Ans)remove( )
b)Delete 3rd element from the list. Ans)pop(2)
c)Add an element in the end of the list. Ans)append( )
d)Add an element in the beginning of the list.
Ans) insert(0,element)
e)Add elements of a list in the end of a list. Ans) extend( )
Assignments
Type A : Short Answer Questions / Conceptual Questions
1 Discuss the utility and significance of Lists, briefly.
Lists in python are used to store elements which cannot be stored
in a single variable. They are similar to arrays from other
programming languages. They are mutable and hence their
elements can be changed. Also they can store elements of different
data types
2 What do you understand by mutability? What does “in place”
memory updation mean?
Mutability means that the elements of the object can be changed in
place. “In place” means that the object dosen’t need to change
memory location to store the new changes.Eg
L=[1,2,3,4]
A=id(L)
L[0] = 5
B=id(L)
8 What are list slices? What for can you use them?
List slices are parts of the list.They can be used to get only the
required elements from the list rather than the whole list.
9 Does the slice operator always produce a new list?
The slice operator always produces a copy of the part of the list
that is being slice.
10 Compare list and strings. How are they similar and how are they
different?
11 What do you understand by true copy of a list?
The changes made in one list do not reflect in the other one.
Eg:
A=[1,2,3,4]
B=list(A)
A[0]=5
print(B) #B is still [1,2,3,4] even though A has become [5,2,3,4]
12 An index out of bounds given with a list name causes error, but
not with list slices. Why?
Slicing returns a list , if the indices don’t fall within the range of
the number of elements in the list ,It returns an empty list.
While accessing an element using an index , if the index is out of
range , it cannot return a default value None.
Answer: 1
2
[13, 18, 11, 16, 13, 18, 13, 3]
10 Predict the output
a,b,c =[1,2],[1,2],[1,2]
print(a == b)
Answer : True
11 Predict the output of following two parts. Are the outputs same?
Are the outputs different? Why?
a)L1, L2 =[2,4],[2,4]
L3 = L2
L2[1] = 5
print(L3)
Answer: [2,5]
(Reason is L2 and L3 represents the same memory location)
b)L1,L2 = [2,4] ,[2,4]
L3 = list(L2)
L2[1] = 5
print(L3)
Answer : [2,4]
(Reason is L2 and L3 represents the different memory location)
a) L1=[1,11,21,31]
An=L1.remove(31)
print(An + 2)
14 Find the errors
a)L1=[3,4,5]
L2=L1*3
print(L1* 3.0)
print(L2)
output
523
[]
<class 'list'> <class 'str'>
PROGRAMS
1 Program to find minimum / smallest element from a list of
element along with its index in the list
L=eval(input(“Enter list : ”))
length = len(L)
minele =L[0]
minindx =0
for i in range (1, length):
if L[i] < minele :
minele = L[i]
minindx = i
print(“Given list is :” , L)
print(“The minimum element of the given list is :”)
print(minele,”at index” , minindx)