Chapter – 9 : Lists
Summary (A Quick Recap)
• Lists are mutable sequences in Python, i.e., we can change the elements of the list.
• Elements of a list are put in square brackets separated by comma.
• A list within a list is called a nested list.
• List indexing is same as that of strings and starts at Two way indexing allows traversing the list in the forward as well as in the backward direction.
• Operator + concatenates one list to the end of other list.
• Operator * repeats a list by specified number of times.
• Membership operator in tells if an element is present in the list or not and not in does the opposite.
• Slicing is used to extract a part of the list.
• There are many list manipulation functions including: len(), list(), append(), extend(), insert(), count(), find(), remove(), pop(), reverse(), sort(), sorted(), min(), max(), sum().
• List indexing is same as that of strings and starts at Two way indexing allows traversing the list in the forward as well as in the backward direction.
• Operator + concatenates one list to the end of other list.
• Operator * repeats a list by specified number of times.
• Membership operator in tells if an element is present in the list or not and not in does the opposite.
• Slicing is used to extract a part of the list.
• There are many list manipulation functions including: len(), list(), append(), extend(), insert(), count(), find(), remove(), pop(), reverse(), sort(), sorted(), min(), max(), sum().
Class 11 Computer Science – NCERT Book Exercise Solution
Chapter 9. Lists
1. What will be the output of the following statements?
i. Find the output
list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
Answer: [10, 12, 26, 32, 65, 80]
ii. Find the output
list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
Answer: [10, 12, 26, 32, 65, 80]
[12, 32, 65, 26, 80, 10]
iii. Find the output
list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]
Answer: [10, 8, 6, 4, 2]
[1,2,3,4,5,6,7,8,9,10]
iv. Find the output
list1 = [1,2,3,4,5]
list1[len(list1)-1]
Answer: 5
2. 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])
Answer: Elements of myList after given operations are (i) [10,20,30,40,[50,60]]
(ii) [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])
Answer: Above code print even positioned element of myList.
1
3
5
7
9
4. 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)
Answer: [1, 2, 3]
b. myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del myList[:5]
print(myList)
Answer: [6, 7, 8, 9, 10]
c. myList = [1,2,3,4,5,6,7,8,9,10]
del myList[ : : 2]
print(myList)
Answer: [2, 4, 6, 8, 10]
5. Differentiate between append() and extend() functions of list.
Answer: append() vs extend()
1. append() method of list is use to add element at the end of the list.
extend() method of list is use to add a list at the end of another list.
2. append() method increase the size of list by 1 while extend() method increase the size of list by len(list2).
myList = [10,20,30,40]
myList.append([50,60])
print(myList)
myList.extend([80,90])
print(myList)
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
Answer: (a) In list1 * 2, * operator work as replication operator. Which replicate the list1 two times. list1 * 2 will generate a list [6, 7, 8, 9, 6, 7, 8, 9]. But list1 is not changed.
(b) list1 *= 2, in this *= is a replication and assignment operator. It means it replicate the list1 two times and update list1. New list1 is [6, 7, 8, 9, 6, 7, 8, 9]
(c) list1 = list1 * 2, this statement will create a new list1 containing the two times replicated value of list1.
Lets understand this with an example.
>>> list1 = [6, 7, 8, 9] >>> id(list1) 33379432 >>> list1 * 2 [6, 7, 8, 9, 6, 7, 8, 9] >>> list1 [6, 7, 8, 9] >>> id(list1) 33379432 >>> list1 *= 2 >>> list1 [6, 7, 8, 9, 6, 7, 8, 9] >>> id(list1) 33379432 >>> list1 = list1 * 2 >>> list1 [6, 7, 8, 9, 6, 7, 8, 9, 6, 7, 8, 9, 6, 7, 8, 9] >>> id(list1) 64129512 >>>
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’
Answer: Statements are –
(a) print(” Percentage of the student : “, stRecord[3])
b) print(“Marks in the fifth subject : “, stRecord[2][4])
c) print(“Maximum marks of the student :”, max(stRecord[2]))
d) print(“Roll no. of the student :”, stRecord[1])
e) stRecord[0] = ‘Raghav’