Class 11 Computer Science Ch 9 Lists in Python NCERT Book Exercise Solution


Programming Problems


1. Write a program to find the number of times an element occurs in the list.

Answer:

list1 = [2, 5, 1, 6, 7, 2, 9, 2]

n = int(input("Number to count occurrence : "))
t = list1.count(n)
print(n, "occurs", t, "times")

Number to count occurrence : 2
2 occurs 3 times


2. Write a program to read a list of n integers (positive having all positive numbers and the other having all negative numbers from the given list. Print all three lists.

Answer:

n = int(input("How many elements want in list : "))

list1 = []

for i in range(n):
    num = int(input(""))
    list1.append(num)


positive = []
others = []
for element in list1:
    if element < 0:
        others.append(element)
    else:
        positive.append(element)

print(list1)
print(positive)
print(others)

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

Answer:

def Largest(list1):
    max1 = number[0]
    for n in number:
         if max1 < n :
              max1 =  n
     retrun max1

#using builtin method
def Large(list1):
    retrun max(list1)

number = eval(input("Enter List : "))  # like this [5,10,65,2,15,39,24]
large = Largest(number)  
print("Largest: ", large)

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

Answer:

def secondLargest(list1):
    max1 = number[0]
    max2 = number[0]
    for n in number:
         if max1 < n :
            max2 = max1
            max1 =  n
         elif max2 < n:
             max2 = n
    retrun max2


number = eval(input("Enter List : "))  # like this [5,10,65,2,15,39,24]
large2 = secondLargest(number)  
print("Largest 2 : ", large2)

5. 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. Hint: You can use an built-in function to sort the list
.

Answer:

lst  = eval(input("Enter list : "))
lst.sort()
length = len(lst)
mid = length // 2
if length % 2 != 0:
   median = lst[mid]
else:
   mid1, mid2 = mid-1, mid
   median = (lst[mid1] + lst[mid2]) // 2

print("The Median Value is ", median)

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:

lst = eval(input("Enter List : "))
newlist = []
for num in lst:
   if num not in newlist:
      newlist.append(num)
lst = newlist
print("List without duplicate element : ", lst)

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:

lst = eval(input("Enter List : "))

newelement = int(input("Enter element to be insert  :"))
position = int(input("Enter valid index position "))

lst.insert( position, newelement )
print("List after insertion : ", lst)

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:

lst = eval(input("Enter List : "))

position = int(input("Enter valid index position, to delete element "))

lst.pop(position)
print("List after deletion ", lst)

value = int(input("Enter value to be delete  :"))

lst.remove( value )
print("List after removing element : ", lst)

9. Read a list of n elements. Pass this list to a function which reverses this list in-place without creating a new list.

def reverseList(list1):
    length = len(list1)
    k =  length-1
    for  j in range(length):
        
        list1[j],list1[k] = list1[k], list1[j]
        k = k-1
        if j == k:
            break 

numbers = eval(input("Enter a list : ")) # like this [2,4,6,7]

reverseList(numbers)

print(numbers)

Class 11 NCERT Book Exercise Solution

You cannot copy content of this page

Scroll to Top