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

List_Worksheet practice

The document consists of a worksheet containing various programming exercises focused on list manipulation in Python. It includes questions about the output of specific code snippets, explanations of list comprehension, and tasks for creating and modifying lists. The exercises cover a range of topics such as indexing, slicing, appending, and removing elements from lists.

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

List_Worksheet practice

The document consists of a worksheet containing various programming exercises focused on list manipulation in Python. It includes questions about the output of specific code snippets, explanations of list comprehension, and tasks for creating and modifying lists. The exercises cover a range of topics such as indexing, slicing, appending, and removing elements from lists.

Uploaded by

Saradha S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WORKSHEET – LIST MANIPULATION

1. What will be the output of the following code?

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

2. What will be the output of the following code?

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

3. What will be the output of the following code?

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

4. What will be the output of the following code?

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

5. What will be the output of the following code?

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

6. What will be the output of the following program?

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

7. Explain List Comprehension and an Elegant way to create a new List:

8. What will be the output of the following program?

theList=[1, 2, [1.1, 2.2]]


len(theList)

9. What will be the output of the 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)

10. What will be the output of the following program?

for fruit in ['apple','banana','mango']:


print("I like",fruit)
11. What will be the output of the following program?

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


pow2

12. What will be the output of the following program?

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


odd

13. What will be the output of the following program?

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


l

14. What will be the output of the following program?

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

15. What will be the output of the following program?

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

16. What will be the output of the following program?

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

17. What will be the output of the 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)

18. What will be the output of the 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='')

19. 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%5):
print(j,'#',end='')
print()

20. What will be the output of the following program?

names = ['Hardik', 'Virat', 'Rahul', 'Dhavan']


print(names[-1][-1])

21. What will be the output of the following program?

"Welcome to Python4csip.com".split()

22. What will be the output of the 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)

23. What will be the output of the program?

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


m

24. What will be the output of the following program?

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

25. What will be the output of the 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()

26. What will be the output of the following program?


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

27. What will be the output of the following program?

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

28. What will be the output of the following program?

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

29. What will be the output of the following program?

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

30. What will be the output of the following program?

List = [True, 50, 10]


List.insert(2, 5)
print(List, "Sum is: ", sum(List))

31. What will be the output of the following program?

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

32. What will be the output of the following program?

aList = [4, 8, 12, 16]


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

33. What will be the output of the following program?

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

34. What will be the output of the following program?

sampleList = [10, 20, 30, 40, 50]


sampleList.pop()
print(sampleList)
sampleList.pop(2)
print(sampleList)
35. What will be the output of the following program?

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

36. What will be the output of the following program?

aList1 = [123, 'xyz', 'zara', 'abc']


aList1.insert(3,2009)
print("Final List:::", aList1)

37. What will be the output of the 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)

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]

39. Which function is used to reverse objects of list in place?

40. Write a Python program to sum all the items in a list.

41. Write a Python program to get the largest number from a list.

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 .

43. Write a Python program to remove duplicates from a list.

45. Take a list of 10 elements. Split it into middle and store the elements in two
different lists.

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

You might also like