Working with list and Dictionary in Python
Q1. What will be the output of the following statements?
i) list1 = [12,32,65,26,80,10] list1.sort() print(list1)Ans. [10, 12, 26, 32, 65, 80]ii) list1 = [12,32,65,26,80,10] sorted(list1) print(list1)Ans. [12, 32, 65, 26, 80, 10]iii) list1 = [1,2,3,4,5,6,7,8,9,10] list1[: : -2] print(list1[ : 3] + list1[ 3 : ])Ans. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]iv) list1 = [1,2,3,4,5] print( list1 [ len ( list1 ) - 1 ] )Ans. 5#In Book no print statement is given for part (iii) and (iv) so actually NO OUTPUT, I am using the print statement in last line of both the parts and writing output accordingly
Working with list and Dictionary in Python
Q2. Consider the following list myList. What will be the elements of myList after the following two operations:
myList = [10,20,30,40]
i. myList.append([50,60])
ii. myList.extend([80,90])
Ans. i) [10, 20, 30, 40, [50, 60] ] ii) [10, 20, 30, 40, [50 , 60] , 80 , 90]
Q3. What will be the output of the following code segment:
myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0, len(myList)):
if i % 2 == 0:
print(myList[i])Ans. 1 3 5 7 9
Working with list and Dictionary in Python
Q4. What will be the output of the following code segment:
a) myList = [1,2,3,4,5,6,7,8,9,10] del myList[3:] print(myList)Ans. [1, 2, 3]b) myList = [1,2,3,4,5,6,7,8,9,10] del myList[:5] print(myList)Ans. [6, 7, 8, 9, 10]c) myList = [1,2,3,4,5,6,7,8,9,10] del myList[::2] print(myList)Ans. [2, 4, 6, 8, 10]
Q5. Differentiate between append() and extend() functions of list.
Ans.append( ) extend( ) This function add a single element passed as an
argument at the end of the list. The single element can also be a listThis function add all the elements of the list
passed as an argument at the end of the given listfor example:
list1 = [10,20,30,40]
list1.append(50)
list1
[10, 20, 30, 40, 50]
list1 = [10,20,30,40]
list1.append([50,60])
list1
[10, 20, 30, 40, [50, 60]]for example :
list1 = [10,20,30]
list2 = [40,50]
list1.extend(list2)
list1
[10, 20, 30, 40, 50]
Q6. Consider a list: list1 = [6, 7, 8, 9]
What is the difference between the following operations on list1:
a. list1 * 2
b. list1 *= 2
c. list1 = list1 * 2
Ans. a) This statement will print elements of list1 twice but it does not make any change to list1. b) This statement will twice the elements in list1 ie list1 will have [6, 7, 8, 9, 6, 7, 8, 9] c) It produces the same result as statement b)
Working with list and Dictionary in Python
Q7. The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list: stRecord = [ ‘Raman’, ‘A-36’ , [56, 98, 99, 72, 69], 78.8 ]
Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student
b) Marks in the fifth subject
c) Maximum marks of the student
d) Roll no. of the student
e) Change the name of the student from โRamanโ to โRaghavโ
Ans. a) print(stRecord[3] b) print(stRecord[2][4] c) print(max(stRecord[2] ) ) d) print(stRecord[1] e) stRecord[0] = ‘Raghav’ # I used the print statement to verify the result otherwise you can write without print statement
Q8. Consider the following dictionary stateCapital:stateCapital = {“Assam”:”Guwahati”, “Bihar”:”Patna”, “Maharashtra”:”Mumbai”, “Rajasthan”:”Jaipur”}
Find the output of the following statements:
a) print(stateCapital.get(“Bihar”))
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print(“Maharashtra” in stateCapital)
g) print(stateCapital.get(“Assam”))
h) del stateCapital[“Assam”]
print(stateCapital)
Ans. a) Patna b) dict_keys(['Assam', 'Bihar', 'Maharashtra', 'Rajasthan']) c) dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur']) d) dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')]) e) 4 f) True g) Guwahati h) {'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
Working with list and Dictionary in Python
PROGRAMMING PROBLEMS
Working with list and Dictionary in Python
Q1. Write a program to find the number of times an element occurs in the list.
Ans. L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 4, 5, 2, 4] e = int(input("Enter element to count : ")) print("Element ", e, " occurs", L.count(e)," times in List") OUTPUT : Enter element to count : 2 Element 2 occurs 3 times in List
Q2. Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all
three lists.
Ans. n = int(input("How many elements you want to enter in a list?")) ol = [ ] pl = [ ] nl = [ ] for i in range(n): e = int(input("Enter the element")) ol.append(e) for i in range(len(ol)): if ol[i] >= 0: pl.append(ol[i]) else: nl.append(ol[i]) print("Original List is : " , ol) print("Positive List is : " , pl) print("Negative List is : " , nl) OUTPUT: How many elements you want to enter in a list?5 Enter the element : 1 Enter the element : 2 Enter the element : -3 Enter the element : -4 Enter the element : 5 Original List is : [1, 2, -3, -4, 5] Positive List is : [1, 2, 5] Negative List is : [-3, -4]
Working with list and Dictionary in Python
Q3. Write a program to find the largest and the second largest elements in a given list of elements.
Ans. L = [1, 23, 43, 21, 3, 76, 54, 43, 45] L.sort() print("Largest Element is : ", L[-1]) print("Second Largest Element is : ", L[-2]) OUTPUT: Largest Element is : 76 Second Largest Element is : 54
Q4. Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.
Ans. L = [1,23, 47, 21, 3, 76, 54, 43, 45, 100] L.sort() len = len(L) if len%2 == 0: med=(L[len//2] + L[len//2-1])/2 else : med = L[len//2] print("Median of given list is : ", med) OUTPUT : Median of given list is : 44.0
Working with list and Dictionary in Python
Q5. Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements, i.e., all elements occurring multiple times in the list should appear only once.
Ans. n = int(input("How many elements you want to enter in a list?")) ol=[ ] for i in range(n): e = int(input("Enter the element : ")) ol.append(e) len = len(ol) ol.sort() c=0 print("Oriiinal List is : ", ol) while(c<len-1): if ol[c] == ol[c+1]: ol.pop(c) len=len-1 else: c=c+1 print("List after removing duplicate Values is : ", ol) OUTPUT : How many elements you want to enter in a list?7 Enter the element : 1 Enter the element : 2 Enter the element : 3 Enter the element : 2 Enter the element : 3 Enter the element : 4 Enter the element : 4 Original List is : [1, 2, 2, 3, 3, 4, 4] List after removing duplicate Values is : [1, 2, 3, 4]
Working with list and Dictionary in Python
Q6. Write a program to create a list of elements. Input an element from the user that has to be inserted in the list. Also input the position at which it is to be inserted.
Ans. L = [1, 23, 43, 21, 3, 76, 54, 43, 45] elem = int(input("Enter the number/element to be inserted")) pos = int(input("Enter te posiion where the element to be inserted")) L.insert(pos-1, elem) print(L) OUTPUT : Enter the number/element to be inserted100 Enter te posiion where the element to be inserted1 [100, 1, 23, 43, 21, 3, 76, 54, 43, 45]
Q7. Write a program to read elements of a list and do the following.
a) The program should ask for the position of the element to be deleted from the list and delete the element at the desired position in the list.
b) The program should ask for the value of the element to be deleted from the list and delete this value from the list.
Ans. a) L = [1, 23, 43, 21, 3, 76, 54, 43, 45] pos = int(input("Enter te posiion of the element to be deleted")) print("Original List : " , L) L.pop(pos-1) print("List after deleting element : " ,L) OUTPUT : Enter te posiion of the element to be deleted3 Original List : [1, 23, 43, 21, 3, 76, 54, 43, 45] List after deleting element : [1, 23, 21, 3, 76, 54, 43, 45] Ans. b) L = [1, 23, 43, 21, 3, 76, 54, 43, 45] elem = int(input("Enter the element to be deleted")) print("Original List : " , L) L.remove(elem) print("List after deleting element : " ,L) OUTPUT : Enter the element to be deleted76 Original List : [1, 23, 43, 21, 3, 76, 54, 43, 45] List after deleting element : [1, 23, 43, 21, 3, 54, 43, 45]
Working with list and Dictionary in Python
Q8. Write a Python program to find the highest 2 values in a dictionary.
Ans. D = {'A' : 23, 'B' : 56, 'C' : 29, 'D' : 42, 'E' : 78 } val = [ ] v = D.values() for i in v: val.append(i) val.sort() print("Two maximum values are") print(val[-1]) print(val[-2]) OUTPUT: Two maximum values are 78 56
Working with list and Dictionary in Python
Q9. Write a Python program to create a dictionary from a string โw3resourceโ such that each individual character makes a key and its index value for fist occurrence makes the corresponding value in dictionary.
Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}
Ans. str1 = "w3resource" dict = dict() for i in str1: dict[i] = str1.index(i) print("The required dictionary: ") print() print(dict)
Working with list and Dictionary in Python
Q10. Write a program to input your friendsโ names and their Phone Numbers and store them in the dictionary as the key-value pair.
Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
Ans. friends = { } while True: print("1. Add New Contact") print("2. Display all ") print("3. Delete any contact") print("4. Modify/Change Phone Number") print("5. Check if a friend is present or not") print("6. Display in sorted order of names") print("7. Exit") ch = int(input("Enter your choice : ")) if ch == 1 : nm = input("Enter name : ") ph = input("Enter phone number : ") friends[ph] = nm print("Record Added") elif ch == 2 : for i in friends : print("Name : " , friends[i]) print("Contact number : ", i) elif ch == 3 : c = input("Enter the phone number to delete") if c in friends: del friends[c] else: print("No Record found") elif ch == 4: p = input("Enter the contact number to be modified") if p in friends: n = input("Enter the new name") friends[p] = n print("Record Modified") else: print("No Record Found") elif ch == 5: ph = input("Enter the contact number to check") if ph in friends: print("Record Present") else : print("Record not found") elif ch == 6: for i in sorted(friends.values()): print(i) elif ch == 7: break else: print("Enter Value between 0 and 7")
Working with list and Dictionary in Python
Working with list and Dictionary in Python
Disclaimer : I tried to give you the correct answers of “Working with List and Dictionary” , but if you feel that there is/are mistakes in the answers of “Working with List and Dictionary “ given above, you can directly contact me at csiplearninghub@gmail.com.
Working with list and Dictionary in Python
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series








