Data Structure in Liner List
Data Structure in Liner List
TYPE B:
1. Q. Create a list SqLst that stores the doubles of elements of another list NumLst.
Following code is trying to achieve this. Will this code work as desired? What will be
stored in SqLst after following code?
Numlst = [2, 5, 1, 7, 3, 6, 8, 9]
Squst = NumLst * 2
Answer :-
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = [ ]
for i in NumLst :
SqLst .append( i * 2 )
print (SqLst)
3. Q. Modify your previous code so that SqLst stores the doubled numbers in
ascending order.
NumLst = [2, 5, 1, 7, 3, 6, 8, 9]
SqLst = [ ]
for i in NumLst :
SqLst .append( i * 2 )
SqLst.sort()
print (SqLst)
4.Q. Consider a list ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write code using a list
comprehension that takes the list ML and makes a new list that has only the even
elements of this list in it.
newlst = [i for i in ML if i % 2 == 0]
print (newlst)
target1 = []
for number in source:
if number & 1:
target1.append(number)
Answer =
6.Q. Write equivalent for loop for the following list comprehension:
print (gen)
Answer =
(ii) 8, 9, 2, 3, 7, 8
Code:
n = len(s)
t = s[1:n-1]
Answer =
(i)
(ii)
def h_t(NLst):
from_back = NLst.pop
from_front = NLst.pop()
NLst.append(from_front)
NLst.insert(0, from_back)
NLst1 = [ [21, 12], 31 ]
NLst3 = NLst1.copy()
NLst2 = NLst1
NLst2[-1] = 5
NLst2.insert(1,6)
h_t(NLst1)
Answer =
Output:-
print(ages)
print (Elig)
Output:-
L1 = [x ** 2 for x in range(10) if x % 3 == 0]
L2 = L1
print (L1)
print(L2)
L2.remove(len(L2) - 1)
print (L1)
Answer =
[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
11.Q. Predict the output:
def even(n):
return n % 2 == 0
list1 = [1,2,3,4,5,6,7,8,9]
print(evp)
OUTPUT : [2, 4, 6, 8]
x = b[:2]
x.append (10)
print (x)
(ii)b = [[9,6],[4,5],[7,7]]
x = b[:2]
print(x)
13. Q. Find the Error. Consider the following code, which runs correctly
at times but gives error at other times. Find the error and its reason.
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
if ch = 1:
Lst1.append (100)
elif ch == 2:
print(Lst1.index(100))
print(Lst2.index(100))
elif ch == 3:
print (Lst1.pop())
print(Lst2.pop()
Answer =
Error in line 10 and 13, because Lst2 is empty list.
14.Q. Suggest the correction for the error(s) in previous question's code.
15.Q. Find the error. Consider the following code(s) and predict the error(s):
ANSWER:
(i)
Error is that, code should be in the form of list comprehension.
(ii)
Error is that, list comprehension not work in tuple.
Answer =
17.Q. Suggest corrections for the errors in both the previous questions.
Answer =