11 IP 2022 Lists in Python new
11 IP 2022 Lists in Python new
2. True or False:
(a) True (b) False (c) True (d) True (e) False
(f) True (g) False (h) False (i) True (j) True
1. What is a list?
Ans. A list is a mutable sequence of values which can be of any type. Lists are indexed by an integer. Elements
of a list are enclosed in square brackets [ ], separated by commas. Lists allow duplicate values as well. The
elements of a list can be accessed by its index.
2. What are the differences between lists and strings?
Ans. The differences between lists and strings are:
(a) Strings cannot be changed but lists are mutable. They can be changed.
(b) Lists use square brackets unlike strings which are enclosed in single or double quotes.
(c) Strings store single type of elements but lists can store elements of different types.
3. Can lists be used as keys in Python dictionaries? Justify your answer.
Ans. No, lists cannot be used as keys in a dictionary because lists are mutable while keys of the dictionary are
immutable by default.
4. What is the difference between pop() and remove() functions?
Ans. pop() function removes the elementfrom the specified index and also returns the element which was removed.
if no index value is provided in pop(), then the last element is deleted. remove() function is used when we
know the element to be deleted but not the index of the element. It does not return the deleted element.
5. Given below are statements for creating lists. Find errors, if any, and rewrite the correct statements.
(a) L1= 1,6,a,8
(b) L2=(10)
(c) L1=[[0,1,2,3] ['my', 'book']]
(d) L1=[0,1,2,3], [4,5,6]
(e) L1=(['a','b', 'c' [1,2,3,A] )
(f) L1=[Aman,Lakshay,Aushim,Nishant]
Ans. (a) L1= [1,6, 'a',8]
(b) L2=[10]
(c) L1=[[0,1,2,3], ['my', 'book']]
(d) L1=[[0,1,2,3], [4,5,6]]
(e) L1=['a','b','c',[1,2,3,'A']]
(f) L1=[ 'Aman', 'Lakshay', 'Aushim', 'Nishant' ]
6. Suppose L=[ "abc", [6,7,8], 3, 'mouse']
Consider the above list and answer the following:
(a) L[3:] (b) L[::2]
(c) L[1:2] (d) L[1] [1]
Ans. (a) ['mouse'] (b) ['abc', 3]
(c) [[6, 7, 8]] (d) 7
7. Write the most appropriate list method to perform the following tasks:
(a) Delete a given element from the list.
(b) Get the position of an item in the list.
(c) Delete the 3rd element from the list.
(d) Add single element at the end of the list.
(e) Add an element in the beginning of the list.
(f) Add elements in the list at the end of a list.
Ans. (a) remove()
(b) index()
(c) pop()/del
(d) append()
(e) insert()
(f) extend()
8. Give the output of the following:
(a) a=[2,7,9,13,28]
print(a [2:1+a [:2])
(b) values = [ [3, 4, 5, 1 ], [33, 6, 1, 2] ]
for row in values:
row. sort()
for element in row:
print (element, end = " ")
print()
Ans. (a) [9, 13, 28, 2, 7]
(b) 1 3 4 5
1 2 6 33
9. Suppose listl is [3, 4, 5, 20, 5, 25, 1, 3]. What is listl after listl.pop(1)?
Ans. [3, 5, 20, 5, 25, 1, 3]
10. What will the output of the following statements?
(a) listl = [12,32,65,26,80,10]
listl.sort()
print (listl)
(b) listl = [12,32,65,26,80,10]
sorted(listl)
print (listl)
(c) listl = [1,2,3,4,5,6,7,8,9,10]
listl [: :-2]
listl [:3] + listl [3: ]
(d) listl = [1,2,3,4,5]
listl [len (list1)-1]
Ans. (a) [10,12,26,32,65,80]
Explanation:
Line 1: Creation of list with the list object listl and values -12, 32, 65, 26, 80, 10.
Line 2: listl.sort() function sorts all values in ascending order.
Line 3: Finally the list is printed.
(b) [10,12,26,32,65,80]
[12,32,65,26,80,10]
Explanation:
Line 1: Creation of list with the list object listl and values -12, 32, 65, 26, 80, 10.
Line 2: sorted(listl) function sorts all values and displayed the sorted list.
Line 3: Print the listl as it is without modifying the list.
(c) [10,8,6,4,2]
[1,2,3,4,5,6,7,8,9,10]
Explanation:
Line 1: Creation of list with list object listl and values- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
Line 2: List1 [: : -2] reverses the list with skip value -2.
Line 3: As the first slice returns the first three elements and the next slice returns all elements except
the first three elements.
(d) 5
Explanation:
Line 1: Creation of list with list object listl and values -1, 2, 3, 4, 5.
Line 2: The len() function returns total number of elements from the list. So, the len(listl) = 5.
Therefore, the element at list [5 - 1] = list [4] is 5.
11. Differentiate between append() and extend() methods of list.
Ans.
append() extend()
append() function adds one element to a list at a extend() function adds multiple elements to a list
time. at a time.
The multiple values can be added using a list as a If user adds multiple values in the form of list as a
parameter but append adds them as a list only. parameter, it will add as separate values.
Example: Example:
t1=[1,2,3] t1=[1,2,3]
tl . append ( [4,5] ) tl . extend ( [4,5] )
print(tl) print(tl)
The output will be: [1,2,3, [4,5] ] The output will be: [1,2,3,4,5]
(e) []
18. Write the output of the following:
Ll= [500, 600]
L2= [35, 45]
Ll . append (700)
Ll . extend (L2)
Ll . insert (5,2)
print (L1)
print (Ll+L2)
print(L1)
print (L1 . index (35) )
print(L2*2)
Ans. [500, 600, 700, 35, 45, 2]
[500, 600, 700, 35, 45, 2, 35, 45]
[500, 600, 700, 35, 45, 2]
3
[35, 45, 35, 45]
19. Write the output of the following code:
List1=[2,4,6,8,10]
List2=List1
List3=Listl.copy()
Listl [4] =12
List2[3]=14
Listl . remove (2)
print ("Listl",Listl)
print ("List2",List2)
print ("List3",List3)
Ans. List1[4, 6, 14, 12]
List2 [4, 6, 14, 12]
List3 [2, 4, 6, 8, 10]
20. Write a program to perform linear search on the given list:
list = [10,51,2,18,4,31,13,5,23,64,29]
Ans. #Linear Search Implementation
num = [10,51,2,18,4,31,13,5,23,64,29]
pos = 0
print("List elements are: ",end=")
for i in num:
print(i,end=")
print()
find = int(input("Enter the element to search: "))
flag = 0
for i in num:
if (i == find):
flag = 1
pos = num.index(i)
break
if flag == 1:
print("Element found at index:",pos)
else:
print("Element not found")
Output
List elements are: 10 51 2 18 4 31 13 5 23 64 29
Enter the element to search: 18
Element found at index: 3
21. Write the output of the following:
(a) L=[1,2,3,4,5,6,7,8,9,10]
S= [i for i in L if i%2==0]
print(S)
(b) A=[1,2,3,4]
B=[value*3 for value in A]
print(B)
Ans. (a) [2,4,6,8,10]
(b) [3,6,9,12]
22. Write a program to shift every element of a list to circularly right, e.g.,
Input: 1,2,3,4,5,6,7
Output: 4 561237
Ans. 1=[1,2,3,4,5,6,7]
mid=int (len (1) /2)
for i in range (mid) :
1 [i] , 1 [mid+i] =1 [mid+i] , [i]
print (1)
23. Write a program which accepts a number from the user and prints the frequency of the number in the list
LST given as under. If the number is not in the LST, it should print "Number not found".
LST = [3, 21, 5, 6, 3, 8, 21, 6]
Output:
Enter Element: 6
The frequency of number 6 is 2.
Ans. LST = [3, 21, 5, 6, 3, 8, 21, 6]
length = len (LST)
element = int (input ("Enter element: ") )
c=0
for i in range (0, length):
if element == LST[i]:
c=c+1
if c==0:
print ("Number Not found")
else:
print ("The frequency of number", element, "is", c)
24. Write a program in Python to find the maximum and minimum elements in the list entered by the user.
Ans. 1st = []
num = int (input ( 'How many numbers: ' ) )
for n in range (num) :
numbers = int (input ( 'Enter number: 'H
1st. append (numbers)
print ("Maximum element in the list is :", max(lst))
print ("Minimum element in the list is :", min(lst))
Output:
How many numbers: 5
Enter number: 1
Enter number: 88
Enter number: 45
Enter number: 27
Enter number: 10
Maximum element in the list is : 88
Minimum element in the list is : 1
25. Write the output of the following Python program code:
STR1= list ("SNo4")
for i in range (len (STR1) ) :
if i==3:
x=int (i)
x+=x-3
STR1 [i] =x
elif (STR1 [i] .islower () ) :
STR1 [i] = STR1 [i] . upper ()
else:
STR1 [i]=STR1 [i] *2
print(STR1)
Ans. ['SS', 'NN', '0', '3']
26. Write the output of the following Python program code:
L = "neat"
X = 1,11
Ll = []
count=l
for i in L:
if i in ['a','e','i','o','u']:
X = X + i.swapcase()
else:
if((count%2)!=0):
X = X + str((len(L[:count])))
else:
X=X+i
count = count + 1
print(X)
Ans. lEAt
27. WAP to create a list L with integer elements as given and search a particular value in the list. If it exists,
then the function should display "exists", otherwise it should display "Does not exist".
Input: [10,20,3,100,65,87,2]
Output: >>> Enter the number to be searched :75
Does not exist
>>> Enter the number to be searched :100
Exists at position :4
Ans. L=[10,20,3,100,65,87,2]
N=int(input("Enter the number to be searched:"))
for i in range(len(L)):
if type(L[i])==int and L[i]==N:
print("Exists at position:",i+1)
break
else:
print("Does not exist")
32. Consider a list list1= [1, 2,3,4,5].
What would be displayed for the following code:
(a) listl+2
(b) listl*3
(c) list1+ [10]
Ans. (a) TypeError: can only concatenate list (not "int") to list
(b) [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
(c) [1, 2, 3, 4, 5, 10]
33. WAP to display square of an element if it is an integer and change the case if the element is a string.
L = [10, "FUN", 40, "FEW", 50, "FULL"] .
List L contains both numbers and strings.
Ans. L= [ 10 , "FUN",40,"FEW",50,"FULL"]
for i in range(len(L)):
if type (L[i] )==int:
L[i]= L[i]**2
elif type (L [i]==str) :
L[i] = (L[i]).swapcase()
print (L)
Output:
[100, 'fun', 1600, 'few', 2500, 'full']
34. WAP in Python to display those strings which are starting with character 'A' or 'a' from the given list L.
L= [ "RINKU" , "AUSHIM", "VIJAYA", "AKHTAR", "LEENA", "TARUN", "AMAR"]
Ans. L=["RINKU","AUSHIM","VIJAYA","AKHTAR","LEENA","TARUN","AMAR"]
count =0
for i in L:
if i[0] in ('aA'):
count+=1
print(i)
print("Appearing",count,"times")
Output:
AUSHIM
AKHTAR
AMAR
Appearing 3 times
35. WAP to find and display the sum of all the values which are ending with 3 from a list.
Sample list is: [33,13,92,99,3,12]
Output will be 49.
Ans. L=[33,13,92,99,3,12]
sum=0
x=len (L)
for i in range (0,x) :
if type (L [i] ) ==int:
if L[i]%10==3:
sum+=L [i]
print(sum)
36. A list num contains the following elements:
3,21,5,6,14,8,14,3
WAP to swap the content with the next value divisible by 7 so that the resultant list looks like:
[3,5,21,6,8,14,3,14]
Ans. num=[3,21,5,6,14,8,14,3]
L=len (num)
i=0
while i<L:
if num[i]%7==0:
num[i], num[i+1]= num[i+1], num[i]
i=i+2
else:
i=i+1
print(num)
37. WAP to shift the negative numbers to the right and the positive numbers to the left so that the resultant
list looks like:
Original list: [-12,11, -13, -5,6, -7,5,-3,-6]
Output should be: [11,6,5,-6,-3,-7,-5,-13,-12]
Ans. 1=[-12, 11, -13, -5, 6, -7, 5, -3, -6]
n=len (1)
print("Original list:",1)
for i in range (n-1) :
for j in range (n-i-1) :
if l[j]<0:
l[j],l[j+1]=1[j+1],l[j]
print ("After shifting list is :",1)
38. Consider List1=[1,2,3,4,5,6,7,8,9,10]
Write code in Python to:
(a) Remove last three elements from the list.
(b) Add element 23 at index value 10.
(c) Display the list in sorted order.
Ans. (a) del Listl [-3:]
(b) Listl . insert (10,23)
(c) print (Listl.sort())
39. Write a program to display all the state names from a list of states which start with the alphabet M.
Output should be:
Enter the list = ["MP", "UP", "WB", "TN", "MH","MZ","DL", "BH", "RJ", "HR"]
The states with letter M:
MP
MH
MZ
Ans. a=eval (input ( 'Enter the list = '))
L=len (a)
print ( 'The states with letter M : ')
for i in range (L) :
if a[i] [0] in 'Mmt:
print (a [i] )
else:
pass