This page contains the NCERT Informatics Practices class 11 chapter 4 Working With Lists and Dictionaries exercise solutions.. You can find the solutions for the chapter 4 of NCERT class 11 Informatics Practices programming problems. So is the case if you are looking for NCERT class 11 Informatics Practices related topic Working With Lists and Dictionaries questions and answers for the Exercise. If you’re looking for solutions to the programming problems, you can find them at Working With Lists and Dictionaries Programming Problems Solutions.
Exercise
1. What will be the output of the following statements?
a)
list1 = [12, 32, 65, 26, 80, 10]
list1.sort()
print(list1)
b)
list1 = [12, 32, 65, 26, 80, 10]
sorted(list1)
print(list1)
c)
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1[::-2]
list1[:3] + list1[3:]
d)
list1 = [1, 2, 3, 4, 5]
list1[len(list1) - 1]
The following is the output of the given statements:
a) The following are the statement and the output:
list1 = [12, 32, 65, 26, 80, 10]
list1.sort()
print(list1)
Output:
[10, 12, 26, 32, 65, 80]
Explanation: The ‘
sort()
‘ method is called on the ‘list1
‘ which sorts the elements of the list in ascending order. The sorted list is then printed using the ‘print()
‘ statement.b) The following are the statement and the output:
list1 = [12, 32, 65, 26, 80, 10]
sorted(list1)
print(list1)
Output:
[12, 32, 65, 26, 80, 10]
Explanation: In the given code, the ‘
sorted()
‘ function is used to sort the elements of ‘list1
‘ in ascending order. However, the ‘sorted()
‘ function returns a new sorted list and does not modify the original list (unlike the ‘sort()
‘ method which modifies the original list). Therefore, when we print the ‘list1’ after calling ‘sorted(list1)
,’ we get the original unsorted list as output.c) The following are the statement and the output:
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list1[::-2]
list1[:3] = list1[3:]
Output: No Output.
Explanation: The first statement ‘
list[::-2]
‘ creates a new list that includes every second element of ‘list1
‘ in reverse order. Therefore, the output is ‘[10, 8, 6, 4, 2]
‘The second statemnet ‘
list1[:3] + list1[3:]
‘ concatenates two slices of ‘list1
‘, the first slice containing the first theree elements of the list and the second slice containing the remaining elements. When these two slices are concatenated, it will ultimately results in a list that has the same elements as the original list.All this happens internally and as there is no ‘
print
‘ statement, there will not be any output.d) The following are the statement and the output:
list1 = [1, 2, 3, 4, 5]
list1[len(list1) - 1]
Output: No Output.
Explanation: That statement ‘
list1[len(list1)-1]
‘ accesses the last element of the list ‘list1
‘ using the index ‘len(list1)-1
‘. Since the last element of ‘list1
‘ is ‘5
‘, this expression will evaluate to ‘5
‘.All this happens internally and as there is no ‘
print
‘ statement, there won’t be any output in the console.2. Consider the collowing list
myList
. What will be the elements of myList after each of the following operations?myList = [10, 20, 30, 40]
a)
myList.append([50, 60])
b)
myList.extend([50, 60])
a) Elements of
myList
after the operation myList.append([50, 60])
myList.append([50, 60])
Result: Recall that the ‘
append()
‘ method adds the argument as a single element to the end of the list. So, after this operation, myList
will be [10, 20,30, 40, [50, 60]]
b) Elements
myList
after the operation myList.extend([50, 60])
myList.extend([50, 60])
Result: Recall that the ‘
extend()
‘ method adds the elements of the argument to the end of the list, effectively extending the list. So, after this operation myList
will be [10, 20, 30, 40, [50, 60], 80, 90]
3. 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])
The following will be the output of the given code segment:
1
3
5
7
9
Explanation: The code segment iterates over the indices of the list ‘
myList
‘ using a ‘for
‘ loop and the ‘range()
‘ function. The ‘if
‘ statement checks whether the index is even (‘i%2 == 0
‘) and if so, it prints the elements at that index using the ‘print()
‘ function.Since the even indices of ‘
myList
‘ contains the elements ‘1, 3, 5, 7, 9
‘, these elements will be printed in order.4. What will be the output of the following code segement?
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)
a) The following will be the statement and the output:
myList = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]
del myList[3:]
print(myList)
Output:
[1, 2, 3]
Explanation: The statement ‘
del myList[3:]
‘ deletes all the elements in ‘myList
‘ starting from index 3
(recall that 3
is the index of the fourth element), which means elemnets at indices 3
and higher will be removed. Therefore, ‘myList
‘ will contain the elements ‘[1, 2, 3]
‘ and this is what is printed.b) The following are the statement and the output:
myList = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]
del myList[:5]
print(myList)
Output:
[6, 7, 8, 9, 10]
Explanation: The satement ‘
del myList[:5]
‘ deletes all the elements in ‘myList
‘ up to index 5
(note that this does not include index 5
. Also recall that 5
is the index of the sixth element), which means elements at indices 0
, 1
, 2
, 3
and 4
will be removed. Therefore, ‘myList’ will contain the elements ‘[6, 7, 8, 9, 10]
‘ and this will be printed.c) The following are the statement and the output:
myList = [1, 2, 3, 4, 5, 6, 7, 8 ,9, 10]
del myList[::2]
print(myList)
Output:
[2, 4, 6, 8, 10]
Explanation: The statement ‘
del myList[::2]
‘ deletes every second element of ‘myList
‘, starting from index 0
. Therefore, ‘myList
‘ will contain the elements ‘[2, 4, 6, 8, 10]
‘ and this will be printed.5. Differentiate between
append()
and extend()
methods of list.The following are the differences between
append()
and extend()
.Basis
append()
extend()
Usage
Adds a single element to the end of a list.
Appends elements of an iterable (e.g. list or tuple) to the end of a list.
Argument
Accepts a single argument which could be a number or a string or object or a list or a tuple.
Accepts an iterable as an argument.
Result
Increases the size of the list by 1.
Increases the size of the list by the number of elements in the iterable.
Modification of the Original List
Modifies the original list by adding the new element to the end.
Modifies the original list by adding elements of the iterable to the end.
Performance
Since it only nees to add a single element to the list, it is generally faster than
extend()
Since it needs to add each element of the iterable to the list individually, it can be slower than
append()
for larger iterables.6. 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
The following is the explanation of the impact of the given operations on
list1
a)
list1 * 2
Explanation: The operation ‘
list1 * 2
‘ creates a new list by repeating the original list ‘list1
‘ twice, and reeturns the new list as the result. The original list ‘list1
‘ is not modified by this operation.Example:
list1 = [6, 7, 8, 9]
new_list = list1 * 2
print(new_list)# Output: [6, 7, 8, 9, 6, 7, 8, 9]
print(list1) # Output: [6, 7, 8, 9]
b)
list1 *= 2
Explanation: The operation ‘
list1 *= 2
‘ creates a new list by repeating the original list ‘list1
‘ twice, and returns the new list as the result. The original list ‘list1
‘ is replaced by the result.Example:
list1 = [6, 7, 8, 9]
list1 *= 2
print(list1) # Output: [6, 7, 8, 9, 6, 7, 8, 9]
c)
list1 = list1 * 2
Explanation: The operation ‘
list1 = list1 * 2
‘ creates a new list by repeating the original list ‘list1
‘ twice, assigns the new list to the variable ‘list1
‘, and retuns the new list as the result. The original list ‘list1
‘ is replaced by the new list.Example:
list1 = [6, 7, 8, 9]
list1 = list1 * 2
print(list1) # Output: [6, 7, 8, 9, 6, 7, 8, 9]
The following is the summary of the above differences:
S.No.
Operation
Outcome
a)
list1 * 2
Creates a new list by repeating the original list items twice. Original list is not modified. Also, new list is not assigned to
list1
. In otherwords, list1
will still be pointing to the old list.b)
list1 *= 2
Modifies the original list ‘
list1
‘ by extending it with a copy of itself. The original list is changed in place and the operation does not return a new list.c)
list1 = list1 * 2
Creates a new list by repeating the original list two times and then assigns the new list to the same variable name ‘
list1
‘. The original list is not modified and the new list is returned as the result of the operation.The below table gives the differences in a different format which can help the students understand it much better.
Basis
list1 * 2
list1 *= 2
list1 = list1 * 2
Creates New List?
Yes
No
Yes
Modifies the original List?
No
Yes
No
‘
list1
” now contains the modified list?No
Yes (it is same list)
Yes (New list is assigned to ‘
list1
”)7. 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’
The following table represents what each element in the given list represents.
List Item
Name
Roll no
Marks in five subjects
Percentage of marks
Index
0
1
2
3
Value
Raman
A-36
[56, 98, 99, 72, 69]
78.8
The following are the Python statementts to retrive the required information from the list
stRecord
a) Python Statement to retrieve percentage of the student:
# Percentage of the student is the last element in the list
percentage = stRecord[3]
# OR we can access the last element in the list by using the index '-1'
percentage = stRecord[-1]
b) Python Statement to retrive Marks in the fifth subject.
# The marks are provided as a list which is the 3rd element in the list.
# So, we can access the marks list by using the index 2.
# The fifth subject marks in this list can be obtained by using the index 4.
marks_in_5th_subject = stRecord[2][4]
# OR The inner list can also be accessed separately
# by accessing the inner list first and then the fifth element in the innerlist as follows
marksList = stRecord[2]
marks_in_5th_subject = marksList[4]
# also note that as the 5th subject is the last subject, it can also be accessed as
marks_in_5th_subject = marksList[-1]
c) Python Statement to retrieve maximum marks of the student
# First get the marks list (3rd element) and then find the max.
max_marks = max(stRecord[2])
# OR Get the marks List first and then find the maximum
marksList = stRecord[2]
max_marks = max(marksList)
d) Python Statement to get the Roll No. of the student:
# Roll No. is the second element in the given list (index = 1)
roll_no = stRecord[1]
e) Python Statement to Change the name of the student from ‘Raman’ to ‘Raghav’:
# Name of the student is the first element in the list (index = 1)
stRecord[0] = 'Raghav'
8. 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)
a) To find the output of the statement
print(stateCapital.get("Bihar"))
print(stateCapital.get("Bihar"))
Output:
Patna
Explanation: The ‘
get
‘ method returns the value corresponding to the key passed as the argument. In the given dictionary, the value of the key ‘"Bihar"
‘ is ‘Patna
‘b) To find the output of the statement
print(stateCapital.keys())
print(stateCapital.keys())
Output:
dict_keys(['Assam', 'Bihar', 'Maharashra', 'Rajasthan'])
Explanation: The ‘
keys()
‘ method returns a list of keys in the dictionary. The output will not be the same as what you get when you print a normal ‘list’. Note the additional ‘dict_keys
‘ in the output.c) To find the output of the statement
print(stateCapital.values())
print(stateCapital.values())
Output:
dict_values(['Guwahati', 'Patna', 'Mumbai', 'Jaipur'])
Explanation: The ‘
values()
‘ method retuns a list of values in the dictionary. The output will not be the same as what you get when you print a normal ‘list
‘. Note the additional ‘dict_values
‘ in the output.d) To find the output of the statement
print(stateCapital.items())
print(stateCapital.items())
Output:
dict_items([('Assam', 'Guwahati'), ('Bihar', 'Patna'), ('Maharashtra', 'Mumbai'), ('Rajasthan', 'Jaipur')])
Explanation: The ‘items()’ method retuns a ‘
list
‘ of ‘tuples
‘. The output will not be the same as what you get when you print a normal ‘list
‘. Note the additional ‘dict_items
‘ in the output.e) To find the output of the statement
print(len(stateCapital))
print(len(stateCapital))
Output:
4
Explanation: The ‘
len()
‘ method returns the length or number of key: value pairs of the dictionary passed as the argument. As there’re four key: value pairs in the given dictionary ‘stateCapital
‘, the output will be ‘4
‘f) To find the output of the statement
print("Maharashtra" in stateCapital)
print("Maharashtra" in stateCapital)
Output:
True
Explanation: The membership operator ‘
in
‘ checks if the key is present in the dictionary. If the key is present in the dictionary it returns ‘True
‘. Otherwise, it returns ‘False
‘g) To find the output of the statement
print(stateCapital.get("Assam"))
print(stateCapital.get("Assam"))
Output:
Guwaahati
Explanation: The ‘
get()
‘ method returns the value corresponding to the key passed as the argument. In the given dictionary ‘stateCapital
‘, as the value for the passed in key ‘Assam
‘ is ‘Guwahati
‘, the output is ‘Guwahati
‘.h) To find the output of the statement
del stateCapital["Assam"]
print(stateCapital)
del stateCapital["Assam"]
print(stateCapital)
Output:
{'Bihar': 'Patna', 'Maharashtra': 'Mumbai', 'Rajasthan': 'Jaipur'}
Explanation: ‘
del
‘ is used to delete the item with teh given key. So, the given statement ‘del stateCapital["Assam"]
‘ deletes the item that has the key ‘Assam
‘. After deleting one item, the dictionary will now have only three items remaining. So, when we print ‘stateCapital
‘, the output contains only three items.