100% found this document useful (1 vote)
5K views9 pages

List Manipulation - Exercise Questions Check Point 7.1

The document provides examples and explanations of various list manipulation operations in Python like accessing and changing list elements, different ways of creating lists, mutable and immutable nature of lists, list methods like append(), pop(), insert() etc. It also contains questions to test the understanding of list concepts and operations through examples.

Uploaded by

Shaku Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
5K views9 pages

List Manipulation - Exercise Questions Check Point 7.1

The document provides examples and explanations of various list manipulation operations in Python like accessing and changing list elements, different ways of creating lists, mutable and immutable nature of lists, list methods like append(), pop(), insert() etc. It also contains questions to test the understanding of list concepts and operations through examples.

Uploaded by

Shaku Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LIST MANIPULATION – EXERCISE QUESTIONS

CHECK POINT 7.1


1. Why are list called mutable types?
Ans. 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?
Ans. 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?
Ans.  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?
Ans. We can have values of any type in a list.
No
5. How are individual elements of lists accessed and changed?
Ans. Individual elements of lists are accessed and changed using the index number.
Example,
L[index]= new value
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?
Ans. 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 amount to if a and b are lists?
Ans. A new list with all the elements of ‘a’ and ‘b’ combined.
10. What does a*b amount to if a and b are lists?
It will raise an error.
11. What does a+b amount to if a is a list and b=5?
It will raise an error.
12. Is a string same as a list of characters? No
13. Which functions can you use to add elements to a list?
Ans. append( ) , extend( ) , insert( )
14. What is the difference between append( ) and insert( ) methods of list?
Ans. 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?
Ans. 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?
Ans. 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>)
17. How does sort( ) work? Give example.
Ans. sort( ) function sorts the items of the list by default in ascending order. It does not create a new list.
List.sort ( )
To sort in descending order
List.sort(reverse =True)
18. What does list.clear( ) function do?
Ans. The clear( ) function removes all elements from the list.
L=[1,2,3]
L.clear( )
print(L)
o/p
[]
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 expressions evaluate to?
Suppose that L is the list
L=[“These”, “are”, “a”, [“few”, “words”], “that”, “we”, “will”, “use”]
(a) L[3:4] [[“few”,”words”]]
L[3:4][0] [“few”, “words”]
L[3:4][0][1] “words”
L[3:4][0][1][2] r

(b) “few” in L False


(c) “few” in L[3] True
(d) [L[1]] + L[3] [“are”,”few”,”words”]
(e) L[4:]
(f) L[0: :2]
4. What does each of the following expressions evaluate to?
Suppose that L is the list
L=[“These”,[“are”, “a”], [“few”, “words”], “that”, “we”, “will”, “use”]
(a) len(L) 7
(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]
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( )

Type A : Short Answer Questions/Conceptual Questions


1. Discuss the utility and significance of Lists, briefly.
Ans. 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
2. What do you understand by mutability? What does “in place” memory updation mean?
Ans. 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)
3. Start with the list [8, 9, 10]. Do the following:
(a) Set the second entry (index 1) to 17 L[1]=17
(b) Add 4,5 and 6 to the end of the list. L.extend([4,5,6])
(c) Remove the first entry from the list. L.pop(0)
(d) Sort the list. L.sort()
(e) Double the list. L*2
(f) Insert 25 at index 3. L.insert(3,25)
4. If a is [1, 2, 3]
(a) What is the difference (if any) between a*3 and [a, a, a]? [1,2,3,1,2,3,1,2,3] [[1,2,3] [1,2,3] [1,2,3]]
(b) Is a*3 equivalent to a+a+a?
(c) What is the meaning of a[1:1]?
(d) What’s the difference between a[1:2] and a[1:1]?
5. What’s a[1:1] if a is a string of at least two characters? And what if string is shorter?
6. What is the purpose of del operator and pop method?
7. What does each of the following expressions evaluate to?
Suppose that L is the list
L=[“These”,[“are”, “a”, “few”, “words”], “that”, “we”, “will”, “use”]
(a) L[1][0: :2]
(b) “a” in L[1][0]
(c) L[ :1] + L[1]
(d) L[2: :2]
(e) L[2][2] in L[1]
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. Compare lists with strings. How are they similar and how are they different?
10. 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]
11. Does the slice operator always produce a new list?
Ans. The slice operator always produces a copy of the part of the list that is being slice
12. An index out of bounds given with a list name causes error, but not with list slices. Why?
Ans. 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.
Type B : Application Based Questions
1. What is the difference between the following two expressions, if lst is given as [1, 3, 5]?
(i) lst*3
(ii) lst*=3
2. Given two lists
L1=[“this”, ‘is’, ‘a’, ‘List’] , L2=[“this”, [“is”, “another”], “List”]
Which of the following expressions will cause an error and why?
(a) L1 == L2 (b) L1.upper() (c) L1[3].upper( )
(d) L2.upper( ) (e) L2[1].upper( ) (f) L2[1][1].upper( )
3. From the previous question, give output of expressions that do not result in error.
4. Given a list L1 = [3, 4.5, 12, 25.7, [2, 1, 0, 5], 88]
(a) Which list slice will return [12, 25.7, [2, 1, 0, 5]]
(b) Which expression will return [2, 1, 0, 5]
(c) Which list slice will return [2, 1, 0, 5]
(d) Which slice will return [4.5, 25.7, 88]

Answer : a)L1[2:5] b)L1[4] c)L1[4:5] d)L1[1: :2]


5. Given a list L1 = [3, 4.5, 12, 25.7, [2, 1, 0, 5], 88], which function can change the list to :
(a) [3, 4.5, 12, 25.7,88]
(b) [3, 4.5, 12, 25.7]
(c) [[2, 1, 0, 5], 88]
6. What will the following code result in?
L1=[1, 3, 5, 7, 9]
print(L1 == L1.reverse( ))
print(L1)
7. Predict the output
my_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]
my_list[2:3] = [ ]
print(my_list)
my_list[2:5]=[ ]
print(my_list)

8. Predict the output


List1 = [13, 18, 11, 16, 13, 18, 13]
print(List1.index(18))
print(List1.count(18))
List1.append(List1.count(13))
print(List1)
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) 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] (b) L1, L2 = [2,4], [2, 4]
L3 =L2 L3 = list(L2)
L2[1]=5 L2[1] = 5
print(L3) print(L3)

Ans: [2,4]
[2,5]
12. Find the errors
L1=[1, 11, 21, 31]
L2 = L1 + 2
L3 = L1 * 2
Idx = L1.index(45)

13. Find the errors


(a) L1 = [1, 11, 21, 31] (b) L1 = [1, 11, 21, 31]
An = L1.remove(41) An = L1.remove(31)
print(An + 2)
14. Find the errors
(a) L1 = [3, 4, 5] (b) L = [3, 3, 8, 1, 3, 0, ’1’, ’0’, ‘2’, ‘e’, ‘w’, ‘e’, ‘r’]
L2 = L1 * 3 print(L1[ : : -1]
print(L1 * 3.0) print(L1[-1: -2: -3]
print(L2) print(L1[-1 : -2 : -3 : -4])

15. What will be the output of the following code?


x = [‘3’, ‘2’, ‘5’]
y=‘‘
while x:
y = y + x[-1]
x = x[ : len(x) -1]
print(y)
print(x)
print(type(x), type(y))

output
523
[]
<class 'list'> <class 'str'>
PROGRAMS

Programs on List manipulation

(1) Program to print elements of a list [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’] in separate lines along with element’s both indexex
(positive and negative)
L = [‘q’, ‘w’, ‘e’, ‘r’, ‘t’, ‘y’]
length = len(L)
for a in range(length):
print(“At indexes”, a, “and”, (a – length), “element :”, L[a])

(2) i. Program to find minimum element from a list of element along with its index in the list (without using built-in
functions).
Lst = eval(input(“Enter List elements : “))
length = len(Lst)
min_ele = Lst[0]
min_index = 0
for i in range(1, length):
if Lst[i] < min_ele:
min_ele = Lst[i]
min_index = i
print(“Given List is : “, Lst)
print(“The minimum element of the given list is : “, min_ele)
print(“ The minimum element “, min_ele, “is at index “, min_index)

(2)ii. Program to find minimum element from a list of element along with its index in the list (using built-in
functions).
Lst = eval(input(“Enter List elements : “))
Lst.sort( )
print(“The minimum element of the given list is : “, Lst[0])

OR
Lst = eval(input(“Enter List elements : “))
small=min(Lst)
pos=Lst.index(small)
print(“The minimum element of the given list is : “, small)
print(“ The minimum element “, min_ele, “is at index “,pos)

(3) Program to calculate mean of a given list of numbers.


Lst = eval(input(“Enter List elements : “))
length = len(Lst)
mean = sum = 0
for i in range(0, length):
sum+=Lst[i]
mean = sum/length
print(“Given list is : “, Lst)
print(“The mean of the given list is : , mean)
(4).i Program to count frequency of a given element in a list of numbers (without using built-in functions).
Lst = eval(input(“Enter List elements : “))
length = len(Lst)
element = int(input(“Enter the element :”))
C=0
for i in range(0, length):
if element == Lst[i]:
C+ = 1
if C == 0:
print(element, “ is not present in the list”)
else:
print(element, “has frequency as”, C, “in given list”)
(4).ii Program to count frequency of a given element in a list of numbers (using built-in functions).
Lst = eval(input(“Enter List elements : “))
element = int(input(“Enter the element :”))
C = Lst.count(element)
print(element, “has frequency as”, C, “in given list”)
(5) Given a list of integers L, write code to calculate and display the sum of all the odd numbers in the list.
L=eval(input(“Enter the list elements:”))
pos=0
sum=0
while pos < len(L):
if L[pos]%2==1:
sum=sum+L[pos]
pos=pos+1
print(“Sum of odd elements=”, sum)
(6) Given two lists, write a program that prints “Overlapped” if they have at least one member in common,
otherwise prints “Separated”. You may use the in operator, but for the sake of exercise, write using loops.
listA=eval(input(“Enter elements in list1 :))
listB=eval(input(“Enter elements in list2 :))
len1=len(listA)
for a in range(len1):
ele=ListA[a]
if ele in listB:
print(“Overlapped”)
break
else:
print(“Separated”)

(7) Write a program to find the second largest number of a list of numbers (without using built-in functions).
Lst=eval(input(“Enter List elements :”))
Length=len(Lst)
large=secondlarge=0
if Lst[0] > Lst[1]:
large=Lst[0]
secondlarge=Lst[1]
else:
large=Lst[1]
secondlarge=Lst[0]
for i in range(1, Length):
if Lst[i] > large:
secondlarge=large
large=Lst[i]
elif Lst[i] > secondlarge:
secondlarge=Lst[i]
print(“Largest number of the list =”, large)
print(“Second largest number of the list =”, secondlarge)
(8) i. Write a program that reverses an array of integers (in place) [without using built in function]
Lst=eval(input(“Enter List elements :”))
Length=len(Lst)
rev=-1
mid=Length//2
for i in range(0,mid):
Lst[0], Lst[rev] = Lst[rev], Lst[i]
rev-=1
print(“List after reversing :”,Lst)

(8) ii Write a program that reverses an array of integers (in place) [using built in function]
Lst=eval(input(“Enter List elements :”))
Lst.reverse( )
print(“List after reversing :”,Lst)

(9) Write a program that inputs two lists and creates a third list that contains all elements of the first followed by
all the elements of the second.
Lst1=eval(input(“Enter elements in List 1 :”))
Lst2=eval(input(“Enter elements in List 2:”))
Lst3=Lst1 + Lst2
print(“Resultant List =”, Lst3)
(10) Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the entries in the list
that are greater than 10 with 10.
Lst=eval(input(“Enter List elements (between 1 and 12) :”))
Length=len(Lst)
for i in range(Length):
if Lst[i]>10:
Lst[i]=10
print(“Resultant List =”, Lst)
(11) Write a program that takes two lists L and M of the same size and adds their elements together to form a new
list N whose elements are sums of the corresponding elements in L and M. For instance, if L=[3,1,4] and
M=[1,5,9], then N should equal [4,6,13]

L=eval(input(“Enter elements in List 1 :”))


M=eval(input(“Enter elements in List 2:”))
size=len(L)
N=[ ]
for i in range(size):
x=L[i] + M[i]
N.append(x)
print(“List L is:”, L)
print(“List M is:”, M)
print(“List N is:”, N)

You might also like