Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Chapter List-Excercise Programs

Uploaded by

nazeema.mca06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Chapter List-Excercise Programs

Uploaded by

nazeema.mca06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

TEXTBOOK EXERCISE PRGORAMS : Page No 205:

Question 1: Write a program to find the number of times an element occurs in the list.
Answer:
Method I: Using the built-in function.
list1 = eval(input("Enter the elements: "))
print("The list is:",list1) #printing the list for the user
inp = int(input("Which element occurrence would you like to count? ")) #asking the element to count
count = list1.count(inp) #using the count function
print("The count of element",inp,"in the list is:",count) #printing the output

Method II: Without Using built-in function and range. OUTPUT:


Enter the elements: [10, 20, 30,
Lst=eval(input("Enter the elements in the list:"))
Ele=eval(input("Enter the elements to count:")) 40, 50, 60, 20, 50, 10]
count=0 The list is: [10, 20, 30, 40, 50,
for a in Lst: 60, 20, 50, 10]
if Ele==a:
count+=1
print("No. of times element found in the list is:", count)

Method III: Without Using built-in function.

Lst=eval(input("Enter the elements in the list:"))


Ele=eval(input("Enter the elements to count:"))
length=len(Lst)
count=0
for a in range(length):
if Ele==Lst[a]:
count+=1
print("No. of times element found in the list is:", count)

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:
Method I: Without using range function.
Lst = eval(input("Enter the elements: (Element can be both positive and negative) "))
plist=[]
nlist=[]
for j in Lst:
if j<0:
nlist.append(j) #Appending negative elements to nlist
elif j>0:
plist.append(j) #Appending positive elements to plist
print("The list with all the elements: ",Lst)
print("The list with positive elements: ",plist)
print("The list with negative elements: ",nlist)

Method I: With using range function.


Lst = eval(input("Enter the elements: (Element can be both positive and negative) "))
plist=[]
nlist=[]
for j in range(len(Lst)):
if Lst[j]<0:
nlist.append(Lst[j]) #Appending negative elements to nlist
PAGE
elif Lst[j]>0:
plist.append(Lst[j]) #Appending positive elements to plist
print("The list with all the elements: ",Lst)
print("The list with positive elements: ",plist)
print("The list with negative elements: ",nlist)

OUTPUT:
Enter the elements: (Element can be both positive and negative)
[5,-12,56,8,-9,5-58,12,8]
The list with all the elements: [5, -12, 56, 8, -9, -53, 12, 8]
The list with positive elements: [5, 56, 8, 12, 8]
The list with negative elements: [-12, -9, -53]

Question 3: Write a function that returns the largest element of the list passed as parameter.
Answer: SAME AS CBSE PRESCRIBED QUESTION 1

Question 4: Write a function to return the second largest number from a list of numbers.
Answer:
val=eval(input("Enter the elements: "))
length=len(val)
OUTPUT:
largest=sec_max=0 Enter the elements: [6,7,3,23,45,11]
for i in range(0, length): The original list is: [6, 7, 3, 23, 45, 11]
if val[i] > largest: The largest number in the list is : 45
sec_max = largest The second largest number in the list is : 23
largest=val[i]
elif val[i] > sec_max:
sec_max=val[i]
print("The original list is: ", val)
print("The largest number in the list is : ", largest)
print("The second largest number in the list is : ", sec_max)

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: We can write the code in 2 ways:
Method I: Using Module.
Method II: Without Using Module or using Loop.

Method II: Using module


import statistics #importing module
lst = eval(input("Enter a list :-")) #Getting elements of the list from user
med=statistics.median(lst)
print("The median value is",med)

Method II: Without using module


list1 = list() #defining empty list
inp = int(input("How many elements do you want to add in the list? ")) #Getting input of number of elements to be added in the
list
for i in range(inp): #Getting the input of elements from user
a = int(input("Enter the elements: "))
list1.append(a)
PAGE
print("The list is",list1)#Printing the original list
list1.sort() #Sorting the list
l = len(list1) #Checking the last index
if(l%2 == 0): #if the number of elements is even, then we have to find average of two middle values
num1 = (l) // 2 #first middle element
num2 = (l // 2) + 1 #second middle element
med_e = (list1[num1 - 1] + list1[num2 - 1]) / 2 #Calculating median as average of the two
print("The median value is",med_e)
else: #if number of elements is odd, then we have to return the element at middle index
middle = (l - 1) // 2
med_o= list1[middle]
print("The median value is",med_o)
OUTPUT:
How many elements do you want to add in the list? 6
Enter the elements: 7
Enter the elements: 8
Enter the elements: 6
Enter the elements: 5
Enter the elements: 4
Enter the elements: 3
The list is [7, 8, 6, 5, 4, 3]
The median value is 5.5

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:
list1 = [] #Defining empty list
inp = int(input("How many elements do you want to add in the list? ")) #Asking for number of elements to be added in the list
for i in range(inp): #Taking the input from user
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1) #Printing the original list
length = len(list1) #Checking the length of list for 'for' loop
newList = [] #Defining a new list for adding unique elements
for a in range(length): #Checking if an element is not in the new List
if list1[a] not in newList: #This will reject duplicate values
newList.append(list1[a])
print("The list without any duplicate element is:",newList)

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 original list entered is: [1, 1, 2, 2, 3, 4]
The list without any duplicate element is: [1, 2, 3, 4]

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.
Answer:
PAGE
list1 = [] #Defining empty list
inp = int(input("How many elements do you want to add in the list? ")) #Getting input for the number of elements to be added in
the list
for i in range(inp): #Taking the input from user
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1) #Printing the list
newList = list1 #Copying the list
i=True
while i==True:
inp = input("Do you want to add any new element to the list? (Y/N) ") #Asking the user if he want to add any element to the
list
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: "))
newList.insert(index,elem) #Using insert() function to add the element at particular index
print("***Element added***")
print("The modified list is: ",newList)
else:
print("The list is:", newList)
i+=1
OUTPUT:
How many elements do you want to add in the list? 4
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
The list entered is: [1, 2, 3, 4]
Do you want to add any new element to the list? (Y/N) Y
Enter the element: 6
Enter the index at which you would like to add the element: 3
***Element added***
The modified list is: [1, 2, 3, 6, 4]
Do you want to add any new element to the list? (Y/N) n
The Modified list is: [1, 2, 3, 6, 4]

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
list1 = list() #Defining empty 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)
print("The list entered is:",list1)
PAGE
i=True
while i==True:
#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)):
ele=list1.pop(index)
print("The element", ele, "of index",index, "is deleted from the list. ")
print("The elements in the list: ", list1)
else:
print("The index is out of range.")
else:
print("The elements in the list: ",list1)
i+=1

OUTPUT:
How many elements do you want to add in the list? 5
Enter the elements: 10
Enter the elements: 45
Enter the elements: 78
Enter the elements: 98
Enter the elements: 5
The list entered is: [10, 45, 78, 98, 5]
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 98 of index 3 is deleted from the list.
The elements in the list: [10, 45, 78, 5]
Do you want to delete any element from the list? (Y/N) y
Enter the index of the element you would like to delete: 8
The index is out of range.
Do you want to delete any element from the list? (Y/N) n
The elements in the list: [10, 45, 78, 5]

b) Program:
list1 = [] #Defining empty list
inp = int(input("How many elements do you want to add in the list? ")) #Taking the number of elements to be added as
input
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1) #Printing the list
i=True
while i==True:
inp = input("Do you want to delete any element from the list? (Y/N) ") #Asking the user if he want to delete any
element from the list
if(inp == 'Y' or inp == 'y'): #if user input is yes
elem = int(input("Enter the element which you would like to delete: "))
#Using remove() function to remove the element
PAGE
for a in list1:
if(a == elem):
list1.remove(elem)
print("The element is deleted from the list. ")
print("The Modified list is:", list1)
break
else:
print("Element not found in the list.",'\n',"Enter the element from the list.")
break
else:
print("The elements in the list are:",list1)
i+=1

OUTPUT:
How many elements do you want to add in the list? 5
Enter the elements: 10
Enter the elements: 20
Enter the elements: 30
Enter the elements: 40
Enter the elements: 50
The list entered is: [10, 20, 30, 40, 50]
Do you want to delete any element from the list? (Y/N) y
Enter the element which you would like to delete: 50
The element is deleted from the list.
The Modified list is: [10, 20, 30, 40]
Do you want to delete any element from the list? (Y/N) y
Enter the element which you would like to delete: 20
The element is deleted from the list.
The Modified list is: [10, 30, 40]
Do you want to delete any element from the list? (Y/N) n
The elements in the list are: [10, 30, 40]

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:
list1 = list()
inp = int(input("How many elements do you want to add in the list? "))
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1)
list1.reverse()
print("Reversed List:",list1)

OUTPUT:
How many elements do you want to add in the list? 5
Enter the elements: 1
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
Enter the elements: 5
The list entered is: [1, 2, 3, 4, 5]
Reversed List: [5, 4, 3, 2, 1] PAGE

You might also like