Worksheet - List
Worksheet - List
pow2 = []
3 What will be the output of following code- for x in range(10):
print([i.lower() for i in "HELLO"]) pow2.append(2 ** x)
Ans:
4 What will be the output of following code- 3
a=[[[1,2],[3,4],5],[6,7]]
a[0][1][1] 9 What will be the output of following program:
my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']
Ans:
print('p' in my_list)
4
print('a' in my_list)
5 What will be the output of following code-
a,b=[3,1,2],[5,4,6] print('c' not in my_list)
a+b
Ans:
Ans:
True
[3, 1, 2, 5, 4, 6] False
True
6 What will be the output of following program:
a=[2,1,3,5,2,4] 10 What will be the output of following program:
a.remove(2) for fruit in ['apple','banana','mango']:
a print("I like",fruit)
Ans:
Ans:
[1, 3, 5, 2, 4] I like apple
1|Page 2|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
Ans:
11 What will be the output of following program:
[1, 4, 6, 8]
pow2 = [2 ** x for x in range(10) if x > 5]
[1, 3, 5, 7]
pow2
14 What will be the output of following program: 18 What will be the output of following program:
odd = [1, 9]
odd.insert(1,3) l=[10,20,30,40,50,60]
print(odd) for i in range(1,6):
odd[2:2] = [5, 7] l[i-1]=l[i]
print(odd) for i in range(0,6):
print(l[i],end='')
Ans:
[1, 3, 9] Ans:
[1, 3, 5, 7, 9]
20 30 40 50 60 60
15 What will be the output of following program: 19 What will be the output of following program:
odd = [1, 3, 5] l=[6,12,18,24,30]
print(odd + [9, 7, 5]) for i in l:
print(["re"] * 3) for j in range(1,i%5):
print(j,'#',end='')
Ans:
[1, 3, 5, 9, 7, 5] print()
['re', 're', 're']
Ans:
16 What will be the output of following program: 1#
odd = [2, 4, 6, 8] 1 #2 #
odd[0] = 1 1 #2 #3 #
print(odd)
3|Page 4|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
Ans:
26 What will be the output of following program:
n
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])
21 What will be the output of following program:
"Welcome to Python4csip.com".split() Ans:
5
Ans:
['Welcome', 'to', 'Python4csip.com']
27 What will be the output of following program:
a="hello"
22 What will be the output of following program: b=list((x.upper(),len(x)) for x in a)
myList = [1, 5, 5, 5, 5, 1] print(b)
max = myList[0]
indexOfMax = 0 Ans:
for i in range(1, len(myList)): [('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1)]
if myList[i] > max:
max = myList[i]
28 What will be the output of following program:
indexOfMax = i
a=[[]]*3
print(indexOfMax)
a[1].append(7)
print(a))
Ans:
Ans:
1
[[7], [7], [7]]
5|Page 6|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
Ans:
37 What will be the output of following program:
53
A = [2, 4, 6, 8,10]
L = len (A)
32 What will be the output of following program: S=0
aList = [4, 8, 12, 16] for I in range (1, L, 2):
aList[1:4] = [20, 24, 28] S+=A[I]
print(aList) print("Sum=",S)
Ans:
[4, 20, 24, 28] Ans:
Sum= 12
33 What will be the output of following program:
l = [None] * 10 38
print(len(l))
Given a Python list, find value 20 in the list, and if it is present, replace it with
Ans: 200. Only update the first occurrence of a value
10
list1 = [5, 10, 15, 20, 25, 50, 20]
34 What will be the output of following program:
sampleList = [10, 20, 30, 40, 50]
sampleList.pop() Expected output:
print(sampleList)
sampleList.pop(2) list1 = [5, 10, 15, 200, 25, 50, 20]
print(sampleList)
Ans:
Ans:
list1 = [5, 10, 15, 20, 25, 50, 20]
[10, 20, 30, 40]
[10, 20, 40] index = list1.index(20)
list1[index] = 200
print(list1)
35 What will be the output of following program:
39 Which function is used to reverse objects of list in place.
resList = [x+y for x in ['Hello ', 'Good '] for y in ['Dear', 'Bye']]
print(resList)
Ans:
Ans:
list.reverse ()
['Hello Dear', 'Hello Bye', 'Good Dear', 'Good Bye']
40 Write a Python program to sum all the items in a list.
7|Page 8|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
print('sum of all the elements in List is :::',sum) 44 Write a program to shift every element of a list to circularly right. E.g.-
41 Write a Python program to get the largest number from a list. INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
Ans:
Ans:
l=[4,5,8,9,40,45,14,17,10]
l=[1,2,3,4,5,6,7]
max =l[0]
mid=int(len(l)/2)
for a in l:
for i in range(mid):
if a > max:
l[i],l[mid+i]=l[mid+i],l[i]
max = a
print(l)
print(max)
45 Take a list of 10 elements. Split it into middle and store the elements in two
42 Write a Python program to count the number of strings where the string length dfferent lists. E.g.-
is 2 or more and the first and last character are same from a given list of
INITIAL list :
strings.
Sample List : ['abc', 'xyz', 'cbc', '121'] 58 24 13 15 63 9 8 81 1 78
Expected Result : 2 After spliting :
Ans: 58 24 13 15 63
c=0 Ans:
for word in l: l=[58,24,13,15,63,9,8,81,1,78]
if len(word) > 1 and word[0] == word[-1]:
mid=int(len(l)/2)
c=c+1
print(c) print(l[ :mid])
9|Page 10 | P a g e
Visit Python4csip.com for more updates Visit Python4csip.com for more updates
l=[10,20,30,40,50,60]
for i in range(len(l)): a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
if(i%2==0): numlst = []
print(l[i],end='#') for num in a:
else: if num < 10:
print(l[i])
numlst.append(num)
Ans:
print(numlst)
10#20
51 What will be the output of following program:
30#40
l=[[i*j for j in range(1,11)] for i in range(7,9)]
50#60
l
Ans:
48 [[7, 14, 21, 28, 35, 42, 49, 56, 63, 70],
What will be the output of the following program: [8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]
l=[10,20,30,40,50,60]
for i in range(len(l)): 52 WAP to display the frequency of each item in a list.
if(i%2==0): Ans:
print(l[i],end='#') L=[10,12,14,17,10,12,15,24,27,24]
else: L1=[]
print(l[i],end='@') L2=[]
for i in L:
Ans:
if i not in L2:
10#20@30#40@50#60@ c=L.count(i)
L1.append(c)
L2.append(i)
print('-----------------------------------------------------------------------')
print('Item','\t\t','frequency')
49 Program to Find Even elements in a List: for i in range(len(L1)):
print(L2[i],'\t\t',L1[i])
Ans: 53 What will be the output of the following program:
l=[6,12,18,24,30]
a = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] for i in l:
evenlst = [] for j in range(1,i%4):
print(j,'#',end='')
for num in a: print()
if num % 2 == 0:
Ans:
evenlst.append(num)
1#
11 | P a g e 12 | P a g e