List Based Questions
List Based Questions
OTQs
Multiple Choice Questions
1 List can contain values of these types 6. Givern a list L=[10, 20, 30, 40, 50, 60, 701, what
(a) integers (b) floats would L[1: 4] return ?
c) lists (d) tuples (a) [10, 20, 30, 40] (b) [20, 30, 40, 50]
(e) all of these (c) [20, 30, 401 (d) [30, 40, 50]
2. Which of the following will create an empty 7.
. Given a list L=[10, 20, 30, 40, 50, 60, 701, what
list 3 would L[2: -2] return ?
(a) L [ ] (b) L = list(0) (a) [10, 20, 30, 40] (b) [20, 30, 40, 50]
(d) L = List(empty)
(c)L =list() () [20, 30, 40] (d) [30, 40, 50]
3. Which of the 8. Given a list L=[10, 20, 30, 40, 50, 60, 70],
following will return the last what
element of a list L with 5 elements ? would L4: -1] return ?
() L[5] (b) LI4] (a) [20, 30, 40] (b) [30, 40, 50]
(c)L-1 (d) L[6] (c)[40, 50, 60] (d) [50, 60, 70]
4.
If L =[1, 2] then L*2 will
yield 9.
9. Given a list L= [10, 20, 30, 40, 50, 60, 70], what
(a) [1, 2] 2 would L[-3 : 99] return ?
(b) [1, 2, 2]
() [1, 1, 2, 2] (d) [1, 2, 1, 2] (a) [20, 30, 40) (b) [30, 40, 501
t Ll =[1, 3, (c) [40, 50, 60] (d) [50, 60, 70]
5] and L2 =
[2, 4, 6] then LI+12
will yield 10. find the last element
To find element of
of list
list namely
10. namely 'smiles'
smiles
(a) [1, 2, 3, 4, 5, in Python, , will be used.
6] (b) [1, 3, 5, 2, 4, 6]
(c)13, 7, 11] (d) [1, 3, 5, (2, 4, 6]] (a) smiles[0] (b) smiles[-1]
(c) smiles[lpos] (d) smiles[:-1]
PRACTICts -X
11. Out of the following, what is correct syntax to 20. Which of the following will alwavs
copy one list into another? list ?
ways return a
(a) listA = listB[ ] (a) max( ) (b) min( )
(b) listA = listB[:] (c) listA = listB[ ( ) (c) sort() (d) sorted()
(d) listA = list(listB) 21. Which of the following can delete an
12. What is printed by the Python code ? from a list if the index of the element is nent
ven?
print(list(range (3))) (a) pop() (b) remove()
(b) [1, 2, 3] (c) del (d) all of these
(a) [0, 1, 2, 3]
(d) 0, 1, 2 22 Which of the following can delete
(c) [0, 1, 2 from a list, if its value is given ?
an
element
nent
13. Which of the following commands will create
a list? () pop() (b) remove()
(a) list1 list() (6) list1 [ (c) del (d) all of these
(c) list1 = list([1, 2, 3]) (d) all of these 23. Which of the following searches for an
element
in a list and returns its index ?
14. What is the output when we execute
list("hello") ? (a)search() (b) find()
(a) ('h', 'e', T, T, '01. (c) index( ) (d) Isearch()
(b) ['hello]. C)[lo']. 24 Which of the following can
copy a list to
(d) ['olleh1 another list?
15. What gets printed ? (a) list() (b) new( )
(d) = operator
names = [ 'Hasan', 'Balwant', 'sean', 'Dia'] (c) copy()
print(names[-1][-1]1) Fill in the Blanks
(a) H (b) n
1. Lists are data types and thus their
(c) Hasan (d) Dia (e) a
values can be changed.
16. What is the output of the following
l = [None] * 10
2. To create an empty list, function canbe
used.
print(len(1)) 3. The operator adds one list to the end of
another list.
(a) 10 (b) 0
() Syntax Error () None 4. The operator replicates a list.
17. Which of the following is a standard Python 5. To check if an element is in list,
operator is used.
library function and not an exclusively list
function ? 6. To delete a list slice from a list, is used.
209
True/False Questions
6. The extend( ) adds à single element to a list.
The list() and
coPy() are the similar functions, 7. The
append() can add an element in the middle
The pop() and remove() are similar functions. of a list.
A = 1 and A = list() will produce the sar 8. The insert() can add an element in the middle
result. of a list.
Lists once created cannot be changed, 9. The del statement can only delete list slices
and not single elements from a list.
Sort a list, sort( ) and sorted( ), both can be
used.
10 The del statement can work similar to the popt
function.
NOTE Answers for Oras are given at the end of the book.
Solved Problems
1 How are lists different from the strings when both are sequences?
SoLUTION. The lists and strings are different in following ways
() The lists are mutable sequences while strings are immutable.
(i1) In consecutive locations, strings storé the individual characters while list stores the
references of its elements
elements
(in) Strings store of
single type elements all characters while lists can store
SoLUTION.
()["a", "b", "o", "c", "p",
"*"]
"p"]
(7) "b", "o", "c",
["a",
Assume the same list: letters = l"a", "b", "o", "C", "PI. What would be aispiayed in a Python shell for eaeu
ofthe following expressions if they are evaluated in the given order ? Ifit would give an error then writeou.
error
( » letters +2 (i) >» letters+[2] (ii) >>» letters += 2
SoLUTION.
() Error (TypeError: can only concatenate list (not "int") to list)
(i)"a', "b', 'o', 'c', 'p', 2]
(it) Error (TypeError: 'int' object is not iterable)
(iv) Nothing will be displayed and no error occurs.
(oa, 'b', 'o', 'c', 'p', 21
(vi) Error (TypeError. can only concatenate list (not "str") to list)
(vii) Nothing will be displayed and no error occurs.
(b) False. The string "few" is part of a list which is an element of the list L, the literal string is
not an element of L.
(c) True. String "few" is part of list, which is at the index 3 of List L ie, L[3].
(d) [L[1]] = [ 'are ]
LI3] = [ 'fed, 'words']
[L[1]]+L[3] =['are', 'few, words]
print (1st1)
SOLUTION. [O, 1, [3]]
[O, 1, [3, 4]1
[o, 1, 2]
11. Consider the below given two similar looking code fragments. Which of these will run properly and which one
will produce error? Exaplain.
(a) l s tlst = [1, 2] (b) » 1st = [1, 2]
l s t = l s t + "12" >»lst + "12"
SOLUTION. The code (a) will produce error because of its line 2: 1st = lst+ "12". Thisis because when
we use +operator with lists, we can only use lists as operands as only listscan be concatenated with
lists using +
The code (b) will not produce any error because lst += "12" does not expand to Ist = lst+ "12" as
else
odds odds +1
i fodds > evens:
print ("Balanced oddity")
(a) What isthisprogram calculating? (b)Whatdoes the programfor thelist [1, 5,2,3, 6,6,9]?
(c) What does the program print for the list [2, 5, 2, 3, 6, 6, 9] ? How can we fix this ?
SOLUTION. (a) The given code is checking whether a list of numbers is 'mostly odd' - i.e, whether
there are more odd numbers than even numbers in the list. (b) Balanced oddity
() The code prints nothing. This is because there is only one print statement, the fix is :
i f odds > evens
print ("Balanced oddity")
else
print ("Unbalanced oddity")
15. Write a program to find the largest/smallest value of asset in a gioven list of assets of a company.
SoLUTION.
val = eval(input( "Enter list of assets: "))
print("The list is:", val)
mx = max(val) Enter 1ist of assets [23, 11, 56, 7, 12, 45]
The list is : [23, 11, 56, 7, 12, 45]
mn=min(val) Biggest ASset: 56
print("Biggest Asset: ", mx)
Smallest Asset :7
print("Smallest Asset: ", mn)
16. Write a program to input a list ofnumbers and swap elements at the even location with the elements at the odd
location.
SoLUTION.
val =
eval(input( "Enter a list: "))
Enter a 1ist [12, 23, 45, 16, 22, 34]
print("Original list is:", val) 34
original 1ist is [12, 23, 45, 16, 22,
S=len(val) 01
ifs%2 !=0: 2 3
S S-1 4 5
for i in range( 0, s, 2) : List after swapping: [23, 12, 16, 45, 34, 22)
print(i, i+1)
val[il, val[i+1] = val[i+1], val[ij
print("List after swapping: ", val)
LIST MANIPULATION
Chopte
6 213
ABC company's assets, iabilities and capital details are available in the form of three lists. Write a program to
oct if the accounting equation holds true for ABC
company or not.
SOLUTION.
assets =
eval(input(°Enter 1ist of assets:"))
1iab =
eval (input ("Enter list of liabilities: "))
cap eval(input("Enter list of capital:"))
sum(assets) Enter 1ist of assets: [5000, 5000]
if ==
sum (1iab)+ sum(cap) Enter 1ist of liabi lities: [1000, 1500, 500]
print("ACcounting eqn is balanced. ")
Enter list of capital: [5000, 2000]
else:
AcCounting eqn is balanced.
print ("Accounting eqn is not balanced.")
Given are the lists containing various sales and costs amounts for rwo companies ABC co. and XYZ co. Wrire
18.
a program to calculate the return-on-sales (ROS) for both the companies and display which company's
management sucessfully cuts costs while increasing revenue. Formula to calculate ROS isas given below
ROS=perating Profit
Net Sales
SOLUTION. abcSal= eval (input ("ABC co's sales list: "))
abcCost eval (input ("ABC co's cost list: "))
xyzSal- eval(input( "XZ co's sales list: "))
xyzCost eval(input("XYZ co's cost list: "))
abcOperProfit = sum(abcSal) -sum(abcCost)
#so add it
sum sum + L[pos]
# the answer
print (sum )
214 INFORMATICS PRACTICES
20. Write a program to input a list of numbers and ask for a new element and its position. Then insert the e
the element
at given position.
Enter 1ist of
integers: [4, 6, 2,
SoLUTION. 81
original list: [4, 6, 2, 8]
lst =eval (input ("Enter list of integers: ")) Enter new element:5
print("original list:", lst) Insert at position? <4:3
el =
int(input("Enter new element: ")) List after insertion: [4, 6, 2, 5,81
ln len(lst)
pos int(input("Insert at position? <" + str(ln) + ": "))
1st.insert (pos, el1)
print("List after insertion: ", lst)
21. Write a program to find the second largest liability of a list of liabilities.
SOLUTION.
lst =
eval (input("Enter list of liabilities:"))
length len(lst)
biggest = secondbiggest = 1st[e] # though logically not faiir
for i in range(1, length):
if lst[i] > biggest:
secondbiggest = biggest
biggest = lst[i]
16. Do functions max( ), min( ), sum( ) work with all types of lists?
17. What is the difference between sort( ) and sorted() ?
(a) Which list slice will return [12, 25.7, [2, 1, 0, 5]] ?
(b) Which expression will return [2, 1, 0, 5] ?
)Which list slice will return [12, 1, 0, 5|1 ?
(d) Which list slice will return [4.5, 25.7, 88]?
Given a list L1 [3, 4.5, 12, 25.7, 12, 1, 0, 5, 881, which function can change the list to
(a) 13, 4.5, 12, 25.7, 88] 6)13, 4.5, 12, 25.7] )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_1ist=['p', 'r'» 'o', 'b', '1', 'e', 'm']| 8. Predict the output:
my_1ist[2:3] = []
Listi [13, 18, 11, 16, 13, 18, 13B
print(mylist) print(Lista.index(18))
my_1ist(2:5] - []
print(List1.count(18))
print (my_list) List1.append(List1.count(13))
print(Listi)
Chapter 6 : LIST MANIPULATION
4, 6])[4]
a, b, c [1,2]. [1, 2], [1,
print(a == b) 2]
print (a is b)
11, Predict the output of
following two parts. Are the outputs same? Are the
(@) L1, L2 =[2, 4], [2, 4] outputs different? Why?
L3 = L2 (b) L1, L2 - [2, 4), [2, 4]
L2[1] 5 L3 list(L2)
print(L3) L2[1] 5
print(L3)
12, Find the errors : 1. L1 =
[1, 11, 21, 31]
2. L2 L1+2
3. L3 L1 *2
4. Idx =
L1.index(45)
13. Find the errors
()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) L1 [3, 3, 8, 1, 3, 0, 1', 'e', '2', 'e',
=
*, 'e, 'r]
L2 L13 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 following code ?
X = [3, '2, '5]
y =''
for a in x :
y =y + x [-1]
Xx [: len(x) -1]
print (y)
print (x)
print (type(x), type(y))
integer between 0 and 100, inclusive, named nums1 using
l6, Complete the code to create a list of every
order.
Python, sorted in increasing command that will append the last
non-empty lists. Write a Python
L e t nums2 and nums3 be two
of nums2.
element of nums3 to the end statements.
result of the following
and predict the
o, Consider the following code
'nom']
['om', 'nom',
bieber
counts [1, 2, 3]
nums = counts
it3
#gives output as : 0 12 24 36 48