NCERT Solutions for Class 11 Science Computer science Chapter 9 Lists are provided here with simple step-by-step explanations. These solutions for Lists are extremely popular among Class 11 Science students for Computer science Lists Solutions come handy for quickly completing your homework and preparing for exams. All questions and answers from the NCERT Book of Class 11 Science Computer science Chapter 9 are provided here for you for free. You will also love the ad-free experience on Meritnation’s NCERT Solutions. All NCERT Solutions for class Class 11 Science Computer science are prepared by experts and are 100% accurate.
Page No 204:
Question 1:
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]
list1[:3] + list1[3:]
iv)list1 = [1,2,3,4,5]
list1[len(list1)-1]
Answer:
i) The sort() method will sort the list in ascending order in place.
OUTPUT:
[10, 12, 26, 32, 65, 80]
ii) The sorted() function takes a list as a parameter and creates a new list consisting of the same elements arranged in sorted order. It doesn’t change the list which is passed as a parameter itself.
OUTPUT:
[12, 32, 65, 26, 80, 10]
iii) The statement in line 2 has a negative step size. This means that the list will be traversed in reversed order with step size 2.
OUTPUT: [10, 8, 6, 4, 2]
The statement in line 3 will print the complete list. i.e. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
iv) This will print the last element of the list. i.e. 5
list1[4]
5
Page No 204:
Question 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:
i) append() function takes only one argument and inserts the element to the end of the list. Here, the argument is a list containing the element 50, 60. Therefore, the output will be: [10, 20, 30, 40, [50, 60]]
ii) extend() function takes a list as an argument and appends each element of the list in the same order. The final output will be: [10, 20, 30, 40, [50, 60],80,90]
Page No 205:
Question 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:
Here, the elements of the list will be printed which are at index such that index%2==0. The index of the list can be represented as:
Element | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Therefore, the output will be the elements at index 0, 2, 4, 6, 8 i.e.
1
3
5
7
9
Page No 205:
Question 4:
What will be the output of the following code segment:
- myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList) - myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList) - myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
Answer:
- The statement in line 2 will delete the elements from index 3 to the last index of the list. Therefore, the output will be [1, 2, 3].
- The statement in line 2 will delete the elements from start (index 0) to index 4 of the list. Therefore, the output will be [6, 7, 8, 9, 10]
- The statement in line 2 will delete the elements from the start to the end of the list with step size 2. i.e it will delete elements starting from index 0, then index 2, index 4, index 6 … and so on.
The output will be the remaining elements i.e. [2, 4, 6, 8, 10]
Page No 205:
Question 5:
Differentiate between functions of list. append() and extend().
Answer:
The append() function takes a single element as an argument and appends it to the list. The argument can be any datatype, even a list. The argument will be added as a single element.
>>>list1 = [10, 20, 30]
>>>list1.append(40)
list1 will now be [10, 20, 30, 40]
>>>list1 = [10, 20, 30]
>>>list1.append([40, 50])
Here [40, 50] is a list which will be considered as a single argument, and will be added to the list at index 3. 'list1' will now be [10, 20, 30, [40, 50]]
The extend() function takes any iterable data type (e.g. list, tuple, string, dictionary) as an argument and adds all the elements of the list passed as an argument to the end of the given list. This function can be used to add more than one element in the list in a single statement.
>>> list1 = [2, 4, 5, 6]
>>> list1.extend((2, 3, 4, 5))
>>> print(list1)
OUTPUT:
[2, 4, 5, 6, 2, 3, 4, 5]
Page No 205:
Question 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:
- The statement will print the elements of the list twice, i.e [6, 7, 8, 9, 6, 7, 8, 9]. However, list1 will not be altered.
- This statement will change the list1 and assign the list with repeated elements i.e [6, 7, 8, 9, 6, 7, 8, 9] to list1.
- This statement will also have same result as the statement 'list1 *= 2'. The list with repeated elements i.e [6, 7, 8, 9, 6, 7, 8, 9] will be assigned to list1.
Page No 205:
Question 7:
stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8]
Write Python statements to retrieve the following information from the list stRecord.
- Percentage of the student
- Marks in the fifth subject
- Maximum marks of the student
- Roll no. of the student
- Change the name of the student from ‘Raman’ to ‘Raghav’
Answer:
List Name | Name | Roll No. | Marks in 5 Subjects | Percentage |
stRecord | Raman | A-36 | [56, 98, 99, 72, 69] | 78.8 |
Index | 0 | 1 | 2
[0, 1, 2, 3, 4] index of the list at index 2 |
3 |
Here, we can see that the 'name' is stored at index 0, 'roll no' at index 1, 'marks of the 5 subjects' is stored at index 2 in a list which contains 5 elements, and 'percentage' at index 3
- Percentage: stRecord[3]
- Marks in 5th Subject: stRecord[2][4]
- Maximum Marks of the Student: max(stRecord[2])
- Roll no. of the student: stRecord[1]
- Change the name from Raman to Raghav: stRecord[0] = "Raghav"
Page No 205:
Question 1:
Write a program to find the number of times an element occurs in the list.
Answer:
Program:
#defining a list
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
#printing the list for the user
print("The list is:",list1)
#asking the element to count
inp = int(input("Which element occurrence would you like to count? "))
#using the count function
count = list1.count(inp)
#printing the output
print("The count of element",inp,"in the list is:",count)
OUTPUT:
The list is: [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
which element occurrence would you like to count? 10
The count of element 10 in the list is: 2
Page No 205:
Question 2:
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.
Answer:
Program:
#Defining empty list
list1 = list()
#Getting the input of number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? (Element can be both positive and negative) "))
#Taking the input of elements to be added
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list with all the elements: ",list1)
#Defining list2 and list3 to store positive and negative elements of the list
list2 = list()
list3 = list()
#Looping through list to segregate positive and negative numbers
for j in range(inp):
if list1[j] < 0:
#Appending negative elements to list3
list3.append(list1[j])
else:
#Appending positive elements to list2
list2.append(list1[j])
print("The list with positive elements: ",list2)
print("The list with negative elements: ",list3)
OUTPUT:
How many elements do you want to add in the list? (Element can be both positive and negative) 5
Enter the elements: -1
Enter the elements: -2
Enter the elements: -3
Enter the elements: 4
Enter the elements: 5
The list with all the elements: [-1, -2, -3, 4, 5]
The list with positive elements: [4, 5]
The list with negative elements: [-1, -2, -3]
Page No 206:
Question 3:
Write a function that returns the largest element of the list passed as parameter.
Answer:
The function can be written in two ways:
1. Using max() function of the list
2. Using for loop to iterate every element and checking for the maximum value
Program 1:
#Using max() function to find largest number
def largestNum(list1):
l = max(list1)
return l
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#Using largestNum function to get the function
max_num = largestNum(list1)
#Printing all the elements for the list
print("The elements of the list",list1)
#Printing the largest num
print("\nThe largest number of the list:",max_num)
OUTPUT:
The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
The largest number of the list: 9
Program 2:
#Without using max() function of the list
def largestNum(list1):
length = len(list1)
num = 0
for i in range(length):
if(i == 0 or list1[i] > num):
num = list1[i]
return num
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#Using largestNum function to get the function
max_num = largestNum(list1)
#Printing all the elements for the list
print("The elements of the list",list1)
#Printing the largest num
print("\nThe largest number of the list:",max_num)
OUTPUT:
The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
The largest number of the list: 9
Page No 206:
Question 4:
Write a function to return the second largest number from a list of numbers.
Answer:
Program:
def secLargestNum(list1):
#Sorting the list in ascending order
list1.sort()
#Returning the second last index of list1
#List with negative indexing will start from the end in reverse order with -1 as the index of the last element
secondLast = list1[-2]
#or len(list1)-2 can be used which will return second last element
#secondLast = list1[len(list1)-2]
return secondLast
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
#Using secLargestNum function to get the function
sec_num = secLargestNum(list1)
#Printing all the elements for the list
print("The elements of the list",list1)
#Printing the second largest num
print("\nThe second-largest number of the list:",sec_num)
OUTPUT:
The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
The second-largest number of the list: 8
Page No 206:
Question 5:
Write a program to read a list of n integers and find their median.
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.
Hint: You can use a built-in function to sort the list
Answer:
Program:
def medianValue(list1):
#Sorting the list
list1.sort()
#Checking the last index
indexes = len(list1)
if(indexes%2 == 0):
#if the number of elements is even, then we have to find average of two middle values
num1 = (indexes) // 2 #first middle element
num2 = (indexes // 2) + 1 #second middle element
#Calculating median as average of the two
med = (list1[num1 - 1] + list1[num2 - 1]) / 2
return med
else:
#if number of elements is odd, then we have to return the element at middle index
middle = (indexes - 1) // 2
med = list1[middle]
return med
#defining empty list
list1 = list()
#Getting input of number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? "))
#Getting the input of elements from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The median value is",medianValue(list1))
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
Enter the elements: 5
Enter the elements: 6
The median value is 3.5
Page No 206:
Question 6:
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.
Answer:
Program:
#function to remove the duplicate elements
def removeDup(list1):
#Checking the length of list for 'for' loop
length = len(list1)
#Defining a new list for adding unique elements
newList = []
for a in range(length):
#Checking if an element is not in the new List
#This will reject duplicate values
if list1[a] not in newList:
newList.append(list1[a])
return newList
#Defining empty list
list1 = []
#Asking for number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#Printing the list without any duplicate elements
print("The list without any duplicate element is:",removeDup(list1))
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 1
Enter the elements: 1
Enter the elements: 2
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
The list entered is: [1, 1, 2, 2, 3, 4]
The list without any duplicate element is: [1, 2, 3, 4]
Page No 206:
Question 7:
Write a program to read 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. Write a user defined function to insert the element at the desired position in the list.
Answer:
Program:
def addElements(list1):
newList = list1
#Asking the user if he want to add any element to the list
inp = input("Do you want to add any new element to the list? (Y/N) ")
#if user input is yes
if(inp == 'Y' or inp == 'y'):
elem = int(input("Enter the element: "))
index = int(input("Enter the index at which you would like to add the element: "))
#Using insert() function to add the element at particular index
newList.insert(index,elem)
print("***Element added***")
addElements(newList) #Calling the addElement function again to check if new element should be added
return newList
#Defining empty list
list1 = []
#Getting input for the number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#Calling the addElement function to get a confirmation about adding the element to the list and then returning the modified list
modList = addElements(list1)
print("The modified list is: ",modList)
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 33
Enter the elements: 44
Enter the elements: 55
Enter the elements: 66
Enter the elements: 77
Enter the elements: 12
The list entered is: [33, 44, 55, 66, 77, 12]
Do you want to add any new element to the list? (Y/N) y
Enter the element: 11
Enter the index at which you would like to add the element: 0
***Element added***
Do you want to add any new element to the list? (Y/N) n
The modified list is: [11, 33, 44, 55, 66, 77, 12]
Page No 206:
Question 8:
Write a program to read elements of a list.
a) The program should ask for the position of the element to be deleted from the list. Write a function to 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. Write a function to delete the element of this value from the list.
Answer:
a) Program:
def deleteElements():
global list1
#Asking the user if he want to delete any element from the list
inp = input("Do you want to delete any element from the list? (Y/N) ")
#if user input is yes
if(inp == 'Y' or inp == 'y'):
elem = int(input("Enter the element which you would like to delete: "))
#Using remove() function to remove the element
for a in list1:
if(a == elem):
list1.remove(elem)
print("The element is deleted from the list. ")
deleteElements()
else:
print("The elements in the list",list1)
#Defining empty list
list1 = []
#Taking the number of elements to be added as input
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#The function delete element is called
deleteElements()
OUTPUT:
How many elements do you want to add in the list? 10
Enter the elements: 1
Enter the elements: 2
Enter the elements: 9
Enter the elements: 8
Enter the elements: 4
Enter the elements: 5
Enter the elements: 7
Enter the elements: 11
Enter the elements: 13
Enter the elements: 15
The list entered is: [1, 2, 9, 8, 4, 5, 7, 11, 13, 15]
Do you want to delete any element from the list? (Y/N) Y
Enter the element which you would like to delete: 11
The element is deleted from the list.
Do you want to delete any element from the list? (Y/N) n
The elements in the list [1, 2, 9, 8, 4, 5, 7, 13, 15]
b) Program:
def deleteElementsAtIndex():
global list1
#Asking the user if he want to delete any element from the list
inp = input("Do you want to delete any element from the list? (Y/N) ")
#if user input is yes
if(inp == 'Y' or inp == 'y'):
index = int(input("Enter the index of the element you would like to delete: "))
#Using pop() function to remove the element at desired index
if(index < len(list1)):
list1.pop(index)
print("The element is deleted from the list. ")
else:
print("The index is out of range.")
deleteElementsAtIndex()
else:
print("The elements in the list",list1)
#Defining empty list
list1 = list()
#Taking the number of elements to be added as input
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#The function deleteElementsAtIndex is called to delete the element after taking user input
deleteElementsAtIndex()
OUTPUT:
How many elements do you want to add in the list? 7
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 7
Enter the elements: 6
Enter the elements: 5
Enter the elements: 4
The list entered is: [1, 2, 3, 7, 6, 5, 4]
Do you want to delete any element from the list? (Y/N) y
Enter the index of the element you would like to delete: 3
The element is deleted from the list.
Do you want to delete any element from the list? (Y/N) n
The elements in the list [1, 2, 3, 6, 5, 4]
Page No 206:
Question 9:
Read a list of n elements. Pass this list to a function which reverses this list in-place without creating a new list.
Answer:
Program:
def reverseList():
global list1
#Using reverse() function to reverse the list in place
#This will not create any new list
list1.reverse()
print("Reversed List:",list1)
#Defining empty list
list1 = list()
#Getting input for number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
#Printing the list
print("The list entered is:",list1)
#The function reverseList is called to reverse the list
reverseList()
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 1
Enter the elements: 2
Enter the elements: 4
Enter the elements: 5
Enter the elements: 9
Enter the elements: 3
The list entered is: [1, 2, 4, 5, 9, 3]
Reversed List: [3, 9, 5, 4, 2, 1]
View NCERT Solutions for all chapters of Class 11