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) ii) list1 = [12,32,65,26,80,10] sorted(list1) print(list1) iii) list1 = [1,2,3,4,5,6,7,8,9,10] list1[: : -2] print(list1[ : 3] + list1[ 3 : ]) iv) list1 = [1,2,3,4,5] print( list1 [ len ( list1 ) - 1 ] ) #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])
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])
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) b) myList = [1,2,3,4,5,6,7,8,9,10] del myList[:5] print(myList) c) myList = [1,2,3,4,5,6,7,8,9,10] del myList[::2] print(myList)
Q5. Differentiate between append() and extend() functions of list.
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
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’
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)
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.
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.
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.
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.
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.
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.
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.
Working with list and Dictionary in Python
Q8. Write a Python program to find the highest 2 values in a dictionary.
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}
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
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