# NCERT Solution Chapter 9 List Questions in Python

Share with others

## NCERT Chapter 9 List Questions in Python

SUMMARY : NCERT CHAPTER 9 LIST Questions in Python

```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 0. 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 Questions 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```

## 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```

## 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]```

Ans.

## Q6. Consider a list: list1 = [6, 7, 8, 9]What is the difference between the following operations on list1:a. list1 * 2b. list1 *= 2c. 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)

## Write Python statements to retrieve the following information from the list stRecord.a) Percentage of the studentb) Marks in the fifth subjectc) Maximum marks of the studentd) Roll no. of the studente) 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

## 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 allthree 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]```

## Q3. Write a function that returns the largest element of the list passed as parameter.

```Ans.

def maxL(a):
return(max(a))
L = [1, 23,  43, 21, 3, 76, 54, 43, 45]
print("Largest element is : ", maxL(L))

OUTPUT :
Largest element is :  76```

## Q4. Write a function to return the second largest number from a list of numbers.

```Ans.

def SL(a):
a.sort()
return(a[-2])
L = [1,23, 43, 21, 3, 76, 54, 43, 45, 100]
print("Second Largest element is : ", SL(L))

OUTPUT :
Second Largest element is :  76```

## Q5. 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

```

## Q6. 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]```

## Q8. 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.

```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)
ol.sort()
ne = int(input("Enter new element : "))
c=0
for i in ol:
c=c+1
if i < ne:
pass
else:
ol.insert(c-1, ne)
break
print("List after inserting new element is  : ", ol)

OUTPUT :

How many elements you want to enter in a list?5
Enter the element : 12
Enter the element : 23
Enter the element : 34
Enter the element : 45
Enter the element : 56
Enter new element : 20
List after inserting new element is  :  [12, 20, 23, 34, 45, 56]```

## Q8. 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.

```Ans. a)

def delposition(L, p):
(L.pop(p-1))
return(L)
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)
print("Original List is  : ", ol)
de = int(input("Enter index value of element to be deleted :"))
nl = delposition(ol, de)
print("List after removing element from specific position is : ",nl)

OUTPUT :

How many elements you want to enter in a list?5
Enter the element : 12
Enter the element : 23
Enter the element : 34
Enter the element : 45
Enter the element : 56
Original List is  :  [12, 23, 34, 45, 56]
Enter index value of element to be deleted : 3
List after removing element from specific position is :  [12, 23, 34, 56]

b)

def delposition(L, p):
(L.remove(p))
return(L)
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)
print("Original List is  : ", ol)
de = int(input("Enter value of element to be deleted :"))
nl = delposition(ol, de)
print("List after removing element from specific position is : ",nl)

OUTPUT :

How many elements you want to enter in a list?5
Enter the element : 1
Enter the element : 7
Enter the element : 9
Enter the element : 4
Enter the element : 6
Original List is  :  [1, 7, 9, 4, 6]
Enter value of element to be deleted : 6
List after removing element from specific position is :  [1, 7, 9, 4]
```

## Q9. Read a list of n elements. Pass this list to a function which reverses this list in-place without creating anew list.

```Ans.

def reverse(L):
L.reverse()
return(L)
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)
print("Original List is  : ", ol)
nl = reverse(ol)
print("List after reversing in place 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]
List after reversing in place is:  [5, 4, 3, 2, 1]```

## List Questions in Python Chapter 9

Disclaimer : I tried to give you the correct “List Questions in Python Chapter 9” , but if you feel that there is/are mistakes in the List Questions in Python Chapter 9 given above, you can directly contact me at csiplearninghub@gmail.com.

Class 12 Computer Science Sample Paper 2020-2021.

Class 12 Computer Science Sample Paper Marking Scheme

Class 12 Computer Science Test Series

Share with others
error: Content is protected !!