Worksheet - List Xi 1
Worksheet - List Xi 1
Worksheet - List Xi 1
Ans:
[1, 3, 2, 1, 3, 2]
Ans:
Ans:
Ans:
Ans:
[3, 1, 2, 5, 4, 6]
[1, 3, 5, 2, 4]
1|P age
7 Explain List Comprehension and Elegant way to create new List:
Ans:
1. List comprehension is an elegant and concise way to create new list from an
existing list in Python.
2. List comprehension consists of an expression followed by for statement inside
square brackets.
3. Here is an example to make a list with each item being increasing power of 2.
pow2 = []
for x in range(10):
pow2.append(2 ** x)
Ans:
3
print('p' in my_list)
print('a' in my_list)
Ans:
True
False
True
Ans:
I like apple
2|Pa ge
I like banana
I like mango
Ans:
[64, 128, 256, 512]
Ans:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Ans:
['Python Language', 'Python Programming', 'C Language', 'C Programming']
Ans:
[1, 3, 9]
[1, 3, 5, 7, 9]
Ans:
[1, 3, 5, 9, 7, 5]
['re', 're', 're']
3|Pa ge
odd[1:4] = [3, 5, 7]
print(odd)
Ans:
[1, 4, 6, 8]
[1, 3, 5, 7]
Ans:
['python', 'list', 1952, 2323, 432]
['list', 1952, 2323]
['list', 1952, 2323, 432]
python
['python', 'list', 1952, 2323, 432, 'python', 'list', 1952, 2323, 432]
['python', 'list', 1952, 2323, 432, 'this', 'is', 'another', 'list']
l=[10,20,30,40,50,60]
for i in range(1,6):
l[i-1]=l[i]
for i in range(0,6):
print(l[i],end='')
Ans:
20 30 40 50 60 60
19 What will be the output of following program:
l=[6,12,18,24,30]
for i in l:
for j in range(1,i%5):
print(j,'#',end='')
print()
Ans:
1#
1 #2 #
1 #2 #3 #
4|Pa ge
20 What will be the output of following program:
names = ['Hardik', 'Virat', 'Rahul', 'Dhavan']
print(names[-1][-1])
Ans:
n
Ans:
['Welcome', 'to', 'Python4csip.com']
Ans:
1
Ans:
Ans:
['a', 'b', 'c', 'd']
5|Pa ge
Ans:
1345
1 2 6 33
Ans:
5
Ans:
[('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1)]
Ans:
[[7], [7], [7]]
Ans:
11
Ans:
[True, 50, 5, 10] Sum is: 66
6|Pa ge
print(T[T[T[6]-2]-4])
Ans:
53
Ans:
[4, 20, 24, 28]
Ans:
10
Ans:
[10, 20, 30, 40]
[10, 20, 40]
Ans:
['Hello Dear', 'Hello Bye', 'Good Dear', 'Good Bye']
Ans:
7|Page
Final List::: [4, 20, 24, 2009, 28]
Ans:
Sum= 12
38
Given a Python list, find value 20 in the list, and if it is present, replace it with
200. Only update the first occurrence of a value
Expected output:
Ans:
list1 = [5, 10, 15, 20, 25, 50, 20]
index = list1.index(20)
list1[index] = 200
print(list1)
39 Which function is used to reverse objects of list in place.
Ans:
list.reverse ()
40 Write a Python program to sum all the items in a list.
Ans:
l=[4,5,8,9,40,45,14,17,10]
sum= 0
for i in l:
sum=sum+i
8|Pa ge
print('sum of all the elements in List is :::',sum)
41 Write a Python program to get the largest number from a list.
Ans:
l=[4,5,8,9,40,45,14,17,10]
max =l[0]
for a in l:
if a > max:
max = a
print(max)
42 Write a Python program to count the number of strings where the string length
is 2 or more and the first and last character are same from a given list of
strings.
Sample List : ['abc', 'xyz', 'cbc', '121']
Expected Result : 2
Ans:
Ans:
l = [10,20,30,20,10,50,60,40,80,50,40]
dup_items =[]
uniq_items = []
for item in l:
if item not in dup_items:
uniq_items.append(item)
dup_items.append(item)
print(dup_items)
9|Pa ge
44 Write a program to shift every element of a list to circularly right. E.g.-
INPUT : 1 2 3 4 5
OUTPUT : 5 1 2 3 4
Ans:
l=[1,2,3,4,5,6,7]
mid=int(len(l)/2)
for i in range(mid):
l[i],l[mid+i]=l[mid+i],l[i]
print(l)
45 Take a list of 10 elements. Split it into middle and store the elements in two
dfferent lists. E.g.-
INITIAL list :
58 24 13 15 63 9 8 81 1 78
After spliting :
58 24 13 15 63
9 8 81 1 78
Ans:
l=[58,24,13,15,63,9,8,81,1,78]
mid=int(len(l)/2)
print(l[ :mid])
print(l[mid: ])
Ans:
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Second largest element is:",a[n-2])
47
What will be the output of the following program:
10 | P a g e
l=[10,20,30,40,50,60]
for i in range(len(l)):
if(i%2==0):
print(l[i],end='#')
else:
print(l[i])
Ans:
10#20
30#40
50#60
8
What will be the output of the following program:
l=[10,20,30,40,50,60]
for i in range(len(l)):
if(i%2==0):
print(l[i],end='#')
else:
print(l[i],end='@')
Ans:
10#20@30#40@50#60@
Ans:
11 | P a g e
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
numlst = []
for num in a:
if num < 10:
numlst.append(num)
print(numlst)
Ans:
[[7, 14, 21, 28, 35, 42, 49, 56, 63, 70],
[8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]
L=[10,12,14,17,10,12,15,24,27,24]
L1=[]
L2=[]
for i in L:
if i not in L2:
c=L.count(i)
L1.append(c)
L2.append(i)
print(' ')
print('Item','\t\t','frequency')
for i in range(len(L1)):
print(L2[i],'\t\t',L1[i])
53 What will be the output of the following program:
l=[6,12,18,24,30]
for i in l:
for j in range(1,i%4):
print(j,'#',end='')
print()
Ans:
1#
1#
1#
12 | P a g e