Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Worksheet - List Xi 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

WORKSHEET – LIST MANIPULATION

1 What will be the output of following code-


list1=[1, 3, 2]
list1 * 2

Ans:

[1, 3, 2, 1, 3, 2]

2 What will be the output of following code-


l="Welcome to Python".split()
l

Ans:

['Welcome', 'to', 'Python']

3 What will be the output of following code-


print([i.lower() for i in "HELLO"])

Ans:

['h', 'e', 'l', 'l', 'o']

4 What will be the output of following code-


a=[[[1,2],[3,4],5],[6,7]]
a[0][1][1]

Ans:

5 What will be the output of following code-


a,b=[3,1,2],[5,4,6]
a+b

Ans:

[3, 1, 2, 5, 4, 6]

6 What will be the output of following program:


a=[2,1,3,5,2,4]
a.remove(2)
a
Ans:

[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 = [2 ** x for x in range(10)]

# Output: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]


print(pow2)

This code is equivalent to:

pow2 = []
for x in range(10):
pow2.append(2 ** x)

8 What will be the output of following program:


theList=[1, 2, [1.1, 2.2]]
len(theList)

Ans:
3

9 What will be the output of following program:


my_list = ['p', 'r', 'o', 'b', 'l', 'e', 'm']

print('p' in my_list)

print('a' in my_list)

print('c' not in my_list)

Ans:

True
False
True

10 What will be the output of following program:


for fruit in ['apple','banana','mango']:
print("I like",fruit)

Ans:
I like apple

2|Pa ge
I like banana
I like mango

11 What will be the output of following program:


pow2 = [2 ** x for x in range(10) if x > 5]
pow2

Ans:
[64, 128, 256, 512]

12 What will be the output of following program:


odd = [x for x in range(20) if x % 2 == 1]
odd

Ans:
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

13 What will be the output of following program:


l=[x+y for x in ['Python ','C '] for y in ['Language','Programming']]
l

Ans:
['Python Language', 'Python Programming', 'C Language', 'C Programming']

14 What will be the output of following program:


odd = [1, 9]
odd.insert(1,3)
print(odd)
odd[2:2] = [5, 7]
print(odd)

Ans:
[1, 3, 9]
[1, 3, 5, 7, 9]

15 What will be the output of following program:


odd = [1, 3, 5]
print(odd + [9, 7, 5])
print(["re"] * 3)

Ans:
[1, 3, 5, 9, 7, 5]
['re', 're', 're']

16 What will be the output of following program:


odd = [2, 4, 6, 8]
odd[0] =1
print(odd)

3|Pa ge
odd[1:4] = [3, 5, 7]
print(odd)

Ans:
[1, 4, 6, 8]
[1, 3, 5, 7]

17 What will be the output of following program:


list1 = ["python", "list", 1952, 2323, 432]
list2 = ["this", "is", "another", "list"]
print(list1)
print(list1[1:4])
print(list1[1:])
print(list1[0])
print(list1 * 2)
print(list1 + list2)

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

18 What will be the output of following program:

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

21 What will be the output of following program:


"Welcome to Python4csip.com".split()

Ans:
['Welcome', 'to', 'Python4csip.com']

22 What will be the output of following program:


myList = [1, 5, 5, 5, 5, 1]
max = myList[0]
indexOfMax = 0
for i in range(1, len(myList)):
if myList[i] > max:
max = myList[i]
indexOfMax = i
print(indexOfMax)

Ans:
1

23 What will be the output of the program?


m = [[x, x + 1, x + 2] for x in range(0, 3)]
m

Ans:

[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

24 What will be the output of following program:


list("a#b#c#d".split('#'))

Ans:
['a', 'b', 'c', 'd']

25 What will be the output of following program:


values = [[3, 4, 5, 1 ], [33, 6, 1, 2]]
for row in values:
row.sort()
for element in row:
print(element, end = " ")
print()

5|Pa ge
Ans:
1345
1 2 6 33

26 What will be the output of following program:


data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(data[1][0][0])

Ans:
5

27 What will be the output of following program:


a="hello"
b=list((x.upper(),len(x)) for x in a)
print(b)

Ans:
[('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1)]

28 What will be the output of following program:


a=[[]]*3
a[1].append(7)
print(a))

Ans:
[[7], [7], [7]]

29 What will be the output of following program:


L1 = list()
L1.append([1, [2, 3], 4])
L1.extend([7, 8, 9])
print(L1[0][1][1] + L1[2])

Ans:
11

30 What will be the output of following program:


List = [True, 50, 10]
List.insert(2, 5)
print(List, "Sum is: ", sum(List))

Ans:
[True, 50, 5, 10] Sum is: 66

31 What will be the output of following program:


T = [1, 2, 3, 4, 5, 6, 7, 8]
print(T[T.index(5)], end = " ")

6|Pa ge
print(T[T[T[6]-2]-4])

Ans:
53

32 What will be the output of following program:


aList = [4, 8, 12, 16]
aList[1:4] = [20, 24, 28]
print(aList)

Ans:
[4, 20, 24, 28]

33 What will be the output of following program:


l = [None] * 10
print(len(l))

Ans:
10

34 What will be the output of following program:


sampleList = [10, 20, 30, 40, 50]
sampleList.pop()
print(sampleList)
sampleList.pop(2)
print(sampleList)

Ans:
[10, 20, 30, 40]
[10, 20, 40]

35 What will be the output of following program:


resList = [x+y for x in ['Hello ', 'Good '] for y in ['Dear', 'Bye']]
print(resList)

Ans:
['Hello Dear', 'Hello Bye', 'Good Dear', 'Good Bye']

36 What will be the output of following program:


aList1 = [123, 'xyz', 'zara', 'abc'];
aList.insert (3,2009)
print ("Final List:::", aList)

Ans:

7|Page
Final List::: [4, 20, 24, 2009, 28]

37 What will be the output of following program:


A = [2, 4, 6, 8,10]
L = len (A)
S=0
for I in range (1, L, 2):
S+=A[I]
print("Sum=",S)

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

list1 = [5, 10, 15, 20, 25, 50, 20]

Expected output:

list1 = [5, 10, 15, 200, 25, 50, 20]

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:

l=['abc', 'xyz', 'cdc', '121']


c=0
for word in l:
if len(word) > 1 and word[0] == word[-1]:
c=c+1
print(c)
43 Write a Python program to remove duplicates from a list.

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

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

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@

49 Program to Find Even elements in a List:

Ans:

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


evenlst = []
for num in a:
if num % 2 == 0:
evenlst.append(num)

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

50 Program to Print list having numbers less than 10


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)

51 What will be the output of following program:


l=[[i*j for j in range(1,11)] for i in range(7,9)]
l

Ans:
[[7, 14, 21, 28, 35, 42, 49, 56, 63, 70],
[8, 16, 24, 32, 40, 48, 56, 64, 72, 80]]

52 WAP to display the frequency of each item in a list.


Ans:

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

You might also like