0% found this document useful (0 votes)
26 views6 pages

Data Structure in Liner List

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Data Structure in Liner List

Uploaded by

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

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 :-

No, This will not stores the doubles of elements.

>SqLst stores :- [2, 5, 1, 7, 3, 6, 8, 9, 2, 5, 1, 7, 3, 6, 8, 9]

2.Q. Change the above code so that it works as stated in previous


question.

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.

ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

newlst = [i for i in ML if i % 2 == 0]

print (newlst)

5.Q. Write equivalent list comprehension for the following code:

target1 = []
for number in source:

if number & 1:

target1.append(number)

Answer =

target1 = [number for number in source if number & 1 ]

6.Q. Write equivalent for loop for the following list comprehension:

gen = [i/2 for i in [0, 9, 21, 32]]

print (gen)

Answer =

for i in [0, 9, 21, 32]:


gen += [i/2]
print (gen)

7.Q. Predict the output of following code if the input is:

(i) 12, 3, 4, 5, 7, 12, 8, 23, 12

(ii) 8, 9, 2, 3, 7, 8

Code:

s = eval (input("Enter a list : "))

n = len(s)

t = s[1:n-1]

print(s[0]==s[n-1] and t.count(s[0])==0)

Answer =

(i)

Enter a list : [12, 3, 4, 5, 7, 12, 8, 23, 12]


False
>>>

(ii)

Enter a list : [8, 9, 2, 3, 7, 8]


True

8. Q. Predict the output:

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)

print(NLst1[0], NLst1[-1], len(NLst1))

print (NLst2[0], NLst2[-1], len(NLst2))

print (NLst3[0], NLst3[-1], len(NLst3))

Answer =

Output:-

<built-in method pop of list object at 0x01CC23F0> 5 4


<built-in method pop of list object at 0x01CC23F0> 5 4
[21, 12] 31 2

9. Q. Predict the output:

ages = [11, 14, 15, 17, 13, 18, 25]

print(ages)

Elig = [x for x in ages if x in range(14, 18)]

print (Elig)

Output:-

Error, because indentation in second line.

10.Q. Predict the output:

L1 = [x ** 2 for x in range(10) if x % 3 == 0]

L2 = L1

L1. append (len(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]

ev = [n for n in list1 if n % 2== 0]

evp = [n for n in list1 if even(n) ]

print(evp)

OUTPUT : [2, 4, 6, 8]

12.Q. Predict the output:

(i)b = [[9,6],[4,5], [7,7]]

x = b[:2]

x.append (10)

print (x)

(ii)b = [[9,6],[4,5],[7,7]]

x = b[:2]

x[1]. append (10)

print(x)

(i)[[9, 6], [4, 5], 10]

(ii)[[9, 6], [4, 5, 10]]

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 = []

print("List1 originally is:", Lst1)

ch = int(input("Enter 1/2/3 and predict which operation was


performed?"))

if ch = 1:

Lst1.append (100)

Lst2. 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.

Correction is that Lst2 should not be empty list.

15.Q. Find the error. Consider the following code(s) and predict the error(s):

(i) y for y in range(100) if y % 2 == 0 and if y % 5 == 0

(ii) (y for y in range(100) if y % 2 == 0 and if y % 5 == 0)

ANSWER:
(i)
Error is that, code should be in the form of list comprehension.

(ii)
Error is that, list comprehension not work in tuple.

16. Q. Find the error in the following list comprehension:

["good" if i<3: else: "better" for i in range(6)]

Answer =

Error is that, there is use of colon (:) in code.

17.Q. Suggest corrections for the errors in both the previous questions.

Answer =

Colon (:) should not be use in code.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy