0% found this document useful (0 votes)
44 views

Worksheet - List

Uploaded by

cs lab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Worksheet - List

Uploaded by

cs lab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Visit Python4csip.com for more updates Visit Python4csip.

com for more updates

WORKSHEET – LIST MANIPULATION


7 Explain List Comprehension and Elegant way to create new List:
1 What will be the output of following code-
list1=[1, 3, 2] Ans:
list1 * 2
1. List comprehension is an elegant and concise way to create new list from an
Ans: existing list in Python.
2. List comprehension consists of an expression followed by for statement inside
[1, 3, 2, 1, 3, 2] square brackets.
3. Here is an example to make a list with each item being increasing power of 2.
2 What will be the output of following code-
l="Welcome to Python".split() pow2 = [2 ** x for x in range(10)]
l
# Output: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
Ans: print(pow2)

['Welcome', 'to', 'Python'] This code is equivalent to:

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: 8 What will be the output of following program:


theList=[1, 2, [1.1, 2.2]]
['h', 'e', 'l', 'l', 'o'] len(theList)

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

I like banana odd[1:4] = [3, 5, 7]


I like mango print(odd)

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

Ans: 17 What will be the output of following program:


[64, 128, 256, 512] list1 = ["python", "list", 1952, 2323, 432]
list2 = ["this", "is", "another", "list"]
print(list1)
12 What will be the output of following program:
print(list1[1:4])
odd = [x for x in range(20) if x % 2 == 1]
print(list1[1:])
odd
print(list1[0])
print(list1 * 2)
Ans:
print(list1 + list2)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Ans:
13 What will be the output of following program: ['python', 'list', 1952, 2323, 432]
l=[x+y for x in ['Python ','C '] for y in ['Language','Programming']] ['list', 1952, 2323]
l ['list', 1952, 2323, 432]
python
Ans: ['python', 'list', 1952, 2323, 432, 'python', 'list', 1952, 2323, 432]
['Python Language', 'Python Programming', 'C Language', 'C Programming'] ['python', 'list', 1952, 2323, 432, 'this', 'is', 'another', 'list']

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

20 What will be the output of following program:


names = ['Hardik', 'Virat', 'Rahul', 'Dhavan'] Ans:
print(names[-1][-1]) 1345
1 2 6 33

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

23 What will be the output of the program?


29 What will be the output of following program:
m = [[x, x + 1, x + 2] for x in range(0, 3)]
L1 = list()
m
L1.append([1, [2, 3], 4])
L1.extend([7, 8, 9])
Ans:
print(L1[0][1][1] + L1[2])
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Ans:
11
24 What will be the output of following program:
list("a#b#c#d".split('#'))
30 What will be the output of following program:
Ans: List = [True, 50, 10]
['a', 'b', 'c', 'd'] List.insert(2, 5)
print(List, "Sum is: ", sum(List))
25 What will be the output of following program:
Ans:
values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
[True, 50, 5, 10] Sum is: 66
for row in values:
row.sort()
for element in row: 31 What will be the output of following program:
print(element, end = " ") T = [1, 2, 3, 4, 5, 6, 7, 8]
print() print(T[T.index(5)], end = " ")

5|Page 6|Page
Visit Python4csip.com for more updates Visit Python4csip.com for more updates

print(T[T[T[6]-2]-4]) Final List::: [4, 20, 24, 2009, 28]

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.

36 What will be the output of following program: Ans:


aList1 = [123, 'xyz', 'zara', 'abc']; l=[4,5,8,9,40,45,14,17,10]
aList.insert (3,2009)
sum= 0
print ("Final List:::", aList)
for i in l:
sum=sum+i
Ans:

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

l=['abc', 'xyz', 'cdc', '121'] 9 8 81 1 78

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])

43 Write a Python program to remove duplicates from a list. print(l[mid: ])

46 Python Program to Find the Second Largest Number in a List


Ans:
Ans:
l = [10,20,30,20,10,50,60,40,80,50,40]
dup_items =[] a=[]
uniq_items = [] n=int(input("Enter number of elements:"))
for item in l: for i in range(1,n+1):
if item not in dup_items: b=int(input("Enter element:"))
uniq_items.append(item) a.append(b)
dup_items.append(item) a.sort()
print(dup_items) print("Second largest element is:",a[n-2])
47
What will be the output of the following program:

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#

print("the new even list is", evenlst) 1#

50 Program to Print list having numbers less than 10 1#


Ans:

11 | P a g e 12 | P a g e

You might also like