# Programming Problems Solutions

This page contains the NCERT Informatics Practices class 11 chapter 4 Working With Lists and Dictionaries programming problems solution. You can find the solutions for the chapter 4 of NCERT class 11 Informatics Practices Programming Problems. So is the case if you are looking for NCERT class 11 Informatics Practices related topic Working With Lists and Dictionaries solutions for Programming Problems. If you’re looking for exercise solutions, you can find them at Working with Lists and Dictionaries Exercise Solutions
PROGRAMMING PROBLEMS
1. Write a program to find the number of times an element occurs in the list.
The following is the program to find the number of times an element occurs in the list.
``````# Request the user to enter the size of the list
list_size = int(input("Enter the size of the list: "))

# Declare an empty list
my_list = []

# Read the elements of the list from the user input.
for i in range(list_size):
my_list.append(input("Enter the element " + str(i + 1) + ": "))

# Request the user to enter the element
# whose number of occurrences in the list should be found out.
element = input("Enter the element for which the number of occurrences should be found: ")
count = my_list.count(element)
print("The number of times the element '" + str(element) + "' occurs in the list is " + str(count) + ".")
``````
Output from Run 1:
``````Enter the size of the list: 5
Enter the element 1: 3
Enter the element 2: 4
Enter the element 3: 3
Enter the element 4: 2
Enter the element 5: 7
Enter the element for which the number of occurrences should be found: 3
The number of times the element '3' occurs in the list is 2
``````
Output from Run 2:
``````Enter the size of the list: 7
Enter the element 1: apple
Enter the element 2: banana
Enter the element 3: orange
Enter the element 4: apple
Enter the element 5: kiwi
Enter the element 6: apple
Enter the element 7: mango
Enter the element for which the number of occurrences should be found: apple
The number of times the element 'apple' occurs in the list is 3
``````
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.
The following is the program to read a list of n integers (positive as well as negative) and create two new lists, one having all positive numbers and the other having all negative numbers from the given list and to print all the three lists.
``````# Prompt the user to input the size of the list.
n = int(input("Enter the size of the list: "))

# Initialize the list to read the integers
my_list = []

for i  in range(n):
integer_element = int(input("Please enter the element " + str(i + 1) + ": "))
my_list.append(integer_element)

# Initialize two lists,
# one to hold positive integers
# and the other to hold negative integers.
positive_integers_list = []
negative_integers_list = []

# Iterate through the my_list
# and add positive integers to the positive integers list
# and the negative numbers to the negative numbers list
for num in my_list:
if num >= 0:
positive_integers_list.append(num)
else:
negative_integers_list.append(num)

# Printing the original list
# and the positive and negative numbers list.
print("The original list is : ", my_list)
print("The positive integers list is : ", positive_integers_list)
print("The negative integers list is : ", negative_integers_list)
``````
Output from Run:
``````Enter the size of the list: 7
Please enter the element 1: -5
Please enter the element 2: 2
Please enter the element 3: 0
Please enter the element 4: -9
Please enter the element 5: 7
Please enter the element 6: -3
Please enter the element 7: 4
The original list is : [-5, 2, 0, -9, 7, -3, 4]
The positive integers list is : [2, 0, 7, 4]
The negative integers list is : [-5, -9, -3]
``````
Note: Note that, in the above program, the number ‘0’ is also added to the positive numbers list.
3. Write a program to find the largest and the second largest elements in a given list of elements.
``````# Prompt the user to input the size of the list.
n = int(input("Enter the size of the list: "))

# Initialize an empty list to hold the user input
elements_list = []

# Prompt the user to enter the list element
# and append the read elements to the list.
for i in range(n):
element = int(input("Enter the element " + str(i + 1) + ": "))
elements_list.append(element)

# Initialize the largest and second largest elements
# to be equal to the first number (element at index 0) in the list
largest_element = elements_list[0]
second_largest_element = elements_list[0]

# Iterate through the elements in the list
# starting from the SECOND ELEMENT i.e., index = 1
# If the element is more than the current largest_element,
# consider the new element as the largest_element
# Otherwise, if it is more than the second largest
# and also not equal to the current largest element
# consider it as the second new largest element
for i in range(1, n):
if elements_list[i] > largest_element:
second_largest_element = largest_element
largest_element = elements_list[i]
elif elements_list[i] > second_largest_element and elements_list[i] != largest_element:
second_largest_element = elements_list[i]

# Output the list, largest element and the smallest element
print("The list is : ", elements_list)
print("The largest element in the list is : ", largest_element)
print("The second largest element in the list is : ", second_largest_element)
``````
Output from Run:
``````Enter the size of the list: 6
Enter the element 1: 5
Enter the element 2: 2
Enter the element 3: 8
Enter the element 4: 1
Enter the element 5: 9
Enter the element 6: 3
The list is : [5, 2, 8, 1, 9, 3]
The largest element in the list is : 9
The second largest element in the list is : 8
``````
4. 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: Use an inbuilt function to sort the list.
The following is the program to read a list of n integers and find their median.
``````# Read the number of integers for which we need to find the median
n = int(input("Enter the size of the list: "))

# Initialize an empty list to hold the numbers
num_list = []

# Prompt the user to enter the numbers in the list
for i in range(n):
num_list.append(int(input("Enter the number " + str(i + 1) + ": ")))

# Sort the list using the built in 'sort()' method
num_list.sort()

# Find the middle index of the list.
# Also note here that we're using the floor division operator.
mid_index = n // 2

# Initialize a variable named 'median' to hold the median value
median = 0
if n % 2 == 0:
# If the list has even number of elements,
# the median is average of middle two numbers.
# One of these numbers will be at (mid_index - 1)
# and the other will be at mid_index
# The average of these two middle numbers will be equal to the median.
median = (num_list[mid_index - 1] + num_list[mid_index])/2
else:
# If the list has odd number of elements,
# the middle number will be the median.
median = num_list[mid_index]

# Print the sorted list
print("Sorted list is : ", num_list)

# Print the calculated median value
print("Median is : ", median)
``````
Here is the output from a sample run of this program:
Output from Run 1:
``````Enter the size of the list: 7
Enter the number 1: 8
Enter the number 2: 5
Enter the number 3: 2
Enter the number 4: 9
Enter the number 5: 4
Enter the number 6: 7
Enter the number 7: 6
Sorted list is : [2, 4, 5, 6, 7, 8, 9]
Median: 6``````
Output from Run 2:
``````Enter the size of the list: 6
Enter the number 1: 5
Enter the number 2: 3
Enter the number 3: 7
Enter the number 4: 9
Enter the number 5: 1
Enter the number 6: 8
Sorted list is : [1, 3, 5, 7, 8, 9]
Median: 6.0``````
5. 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.
The following is the program that would read a list of elements and modifies it so as not to contain any duplicate elements.
``````# Read the number of elements in the list.
n = int(input("Enter the size of the list: "))

# Initialize and empty list to hold the numbers
num_list = []

# Prompt the user to enter the numbers in the list
for i in range(n):
num_list.append(int(input("Enter the number " + str(i + 1) + ": ")))

# Print the initial list
print("The original list is : ", num_list)

# Iterate through the list in the reverse direction
# If an element is found to repeat, delete it.
# (The advantage of iterating in the reverse is that,
# if any element is removed causing the length of the list to change,
# it will not cause any problems in the iteration)
for i in range(len(num_list) - 1, -1, -1): # Iterate through the elements starting from end
for j in range(i - 1, -1, -1) : # Iterate through all the elements before i - 1
if num_list[i] == num_list[j]:
del num_list[i]
break # come out of the inner for loop

# Print the modifed list which now does not contain any duplicates.
print("The list after removing duplicates is : ", num_list)
``````
Output from the Run:
``````Enter the size of the list: 8
Enter the number 1: 1
Enter the number 2: 2
Enter the number 3: 2
Enter the number 4: 3
Enter the number 5: 4
Enter the number 6: 4
Enter the number 7: 4
Enter the number 8: 5
The original list is : [1, 2, 2, 3, 4, 4, 4, 5]
The list after removing duplicates is : [1, 2, 3, 4, 5]
``````
6. Write a program to create 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.
``````# Read the number of elements in the list.
n = int(input("Enter the size of the list: "))

# Initialize and empty list to hold the elements
# (the elements can be numbers or strings or both)
element_list = []

# Prompt the user to enter the numbers in the list
for i in range(n):
element_list.append(input("Enter the element " + str(i + 1) + ": "))

# Print the initial list
print("The input list is : ", element_list)

# Prompt the user to enter the element to insert in the list
# Also, prompt the user to enter the position
# at which this element should be inserted in the list.
element = input("Enter the element to isnert: ")
position = int(input("Enter the position at which the element is to be inserted: "))

# Check whether the entered position is valid one
# If the position is not valid, inform the user to enter a valid position.
if position < 1 or position > n:
print("Please enter a valid position (between 1 and", n, ").")
else:
element_list.insert(position - 1, element)
print("The list after inserting the element at the position", position, "is : ", element_list)
``````
Output from Run 1:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: python
Enter the element 5: numpy
The input list is : [10, 20, 30, 'python', 'numpy']
Enter the element to insert: GPT4
Enter the position at which the element is to be inserted: 2
The list after inserting the element at the specified position is : [10, 'GPT4', 30, 'python', 'numpy']
``````
Output from Run 2:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: 40
Enter the element 5: 50
The input list is : [10, 20, 30, 40, 50]
Enter the element to insert: GPT4
Enter the position at which the element is to be inserted: 7
Please enter a valid position (between 1 and 5)
``````
7. Write a program to read elements of a list and do the following.
a)
The program should ask for the position of the element to be deleted from the list and 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 and delete this value from the list.
a. The following is the program to read elements of a list and ask for the position of the element to be deleted from the list and delete the element at the desired position in the list.
``````# Read the number of elements in the list.
n = int(input("Enter the size of the list: "))

# Initialize an empty list to hold the elements
# (the elements can be numbers or strings or both)
element_list = []

# Prompt the user to enter the numbers in the list
for i in range(n):
element_list.append(input("Enter the element " + str(i + 1) + ": "))

# Print the initial list
print("The input list is : ", element_list)

# Prompt the user to enter the position of the element
# to be deleted from the list.
position = int(input("Enter the position of the element to be deleted from the list: "))

# Check whether the enter position is valid one
# If the position is not valid, inform the user to enter a valid position.
if position < 1 or position > n:
print("Please enter a valid position (between 1 and ", n, ").")
else:
del element_list[position - 1]
print("The list after deleting the element at position ", position, " is : ", element_list)
``````
Output from Run 1:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: python
Enter the element 5: numpy
The input list is : [10, 20, 30, 'python', 'numpy']
Enter the position of the element to be deleted from the list: 2
The list deleting the element at position 2 is : [10, 30, 'python', 'numpy']
``````
Output from Run 2:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: 40
Enter the element 5: 50
The input list is : [10, 20, 30, 40, 50]
Enter the position of the element to be deleted from the list: 7
Please enter a valid position (between 1 and 5)
``````
b. The following is the program to read elements of a list and ask for the value of the element to be deleted from the list and delete this value from the list.
``````# Read the number of elements in the list.
n = int(input("Enter the size of the list: "))

# Initialize and empty list to hold the elements
# (the elements can be numbers or strings or both)
element_list = []

# Prompt the user to enter the numbers in the list
for i in range(n):
element_list.append(input("Enter the element " + str(i + 1) + ": "))

# Print the initial list
print("The input list is : ", element_list)

# Prompt the user to enter the value of the element
# to be deleted from the list.
value = input("Enter the value of the element to be deleted from the list: ")

# Check whether the value is in the list
# If value is not in the list, inform the user to enter a valid value.
if value in element_list:
element_list.remove(value)
print("The list after deleting the value '", value, "' from the list is : ", element_list)
else:
print("The specified value '", value, "' is not found in the list and hence not deleted.")
``````
Output from Run 1:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: python
Enter the element 5: numpy
The input list is : [10, 20, 30, 'python', 'numpy']
Enter the value of the element to be deleted from the list: numpy
The list after deleting the value 'numpy' from the list is : [10, 20, 30, 'python']
``````
Output from Run 2:
``````Enter the size of the list: 5
Enter the element 1: 10
Enter the element 2: 20
Enter the element 3: 30
Enter the element 4: python
Enter the element 5: numpy
The input list is : [10, 20, 30, 'python', 'numpy']
Enter the value of the element to be deleted from the list: GPT4
The specified value 'numpy' is not found in the list and hence not deleted.
``````
8. Write a Python program to find the highest 2 values in a dictionary.
The following is the program to find the highest 2 values in a dictionary:
``````# Define the dictionary
my_dict = {'python': 34, 'numpy': 44, 'cloud': 77, 'IoT': 22, 'AI': 92, 'GPT': 21, 'BARD': 94}

# Get the values of the dictionary.
values = my_dict.values()

# Using the built-in 'sorted' function sort this list
# After this the values list will be sorted in ascending order
sorted_values = sorted(values)

# The last value in the list will be the highest
print("Highest value: ", sorted_values[-1])

# The 'second from the last' value in the list will be the second highest
print("Second Highest value: ", sorted_values[-2])
``````
``````Highest value: 94
Second Highest value: 92
``````
8. Write a Python program to create a dictionary from a string ‘w3resource’ such that each individual character mates makes a key and its index value for fist first occurrence males makes the corresponding value in dictionary.
Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}
``````#Define the given string
w3resource = 'w3resource'

# Define and initialize and empty dictionary
w3resource_dict = {}

# Iterate through the given word using its index
for index in range(len(w3resource)):
char = w3resource[index]
if char not in w3resource_dict: # Add the character and index only if it is not there
w3resource_dict[char] = index # (add it only for the first time)

# print the generated dictionary
print("The generated dictionary is : ", w3resource_dict)
``````
Output from Run:
``````The generated dictionary is : {'w': 0, '3': 1, 'r': 2, 'e': 3, 's': 4, 'o': 5, 'u': 6, 'c': 8}
``````
10. Write a program to input yur friend’s, names and their phone numbers and store them in the dictionary as the key-value pair. Perform the following operations on the dictionary:
a)
Display the Name and Phone Number for all your friends.
b)
Add a new key-value pair in this dictionary and display the modified dictionary
c)
Delete a particular frend from the dictionary
d)
Modify the phone number of an existing friend
e)
Check if a friend is present int eh dictionary or not
f)
Display the dictionary in sorted order of names
``````# Read the 'number' of friends for which you want to enter the details
n = int(input("Enter the number of friends: "))

# Initialize an empty dictionary to hold the name and phone details of friends.
friends = {}
# Prompt the user 'n' times to input the friend's name and phone number.
for i in range(n):
name = input("Enter the name of friend " + str(i + 1) + " : ")
phone = input("Enter the phone number of " + name + " : ")
friends[name] = phone

# Initialize a variable to hold the user choice
choice = ''
while True:
print("Choose one of the following options:")
print("\ta. Display the Name and Phone number for all your firends.")
print("\tb. Add a new friend and his phone number to the friends' dictionary")
print("\tc. Delete a particular friend from the dictionary")
print("\td. Modify the phone number of an existing friend")
print("\te. Check if a friend is present in the dictionary or not")
print("\tf. Display the dictionary in sorted order of names")
print("\tz. Exit")
choice = input("Enter your choice from the options above (a-f or z) : ")
# When the user chooses the option to
# display teh name and phone number for all friends.
if choice == 'a':
print("Name\tPhone Number")
# Iterate through the key, values pairs (name, phone) in the dictionary
# and print them
for name, phone in friends.items():
print(name, "\t", phone)
# When the user chooses the option
# to add a new key-value pair in the dictionary
# and display the modified dictionary
elif choice == 'b':
new_friend_name = input("Enter the name of new firend: ")
new_friend_phone = input("Enter the phone number of new firend" + new_friend_name + ": ")
friends[new_friend_name] = new_friend_phone
print("The following is the modified dictionary after adding a new friend")
print("Name\tPhone Number")
# Iterate through the key, values pairs (name, phone) in the dictionary
# and print them
for name, phone in friends.items():
print(name, "\t", phone)
# When the user chooses an option to
# delete a particular friend from the dictionary
elif choice == 'c':
friend_name_to_delete = input("Enter the name of friend to delete: ")
# Check if the friend is present in the dictionary
# delete the friend only if the friend is already present in the dictionary.
if friend_name_to_delete in friends:
del friends[friend_name_to_delete]
print("Friend", friend_name_to_delete, "is deleted from the dictionary.")
# if the friend is not present in the dictionary,
# inform the user that the friend is not present in the dictionary
# and hence the required operation can not be performed.
else:
print("Friend", friend_name_to_delete, "is not found in the dictionary and hence not deleted.")
# When the user chooses an option to
# modify the phone number of an existing friend
elif choice == 'd':
friend_with_modified_phone = input("Enter the name of the friend whose phone is modified: ")
# check if the friend is already available in the dictionary
# modify the phone number to the new number only if the firend is already present in the dictionary
if friend_with_modified_phone in friends:
modiified_phone = input("Enter the new phone number for friend " + friend_with_modified_phone + " : ")
friends[friend_with_modified_phone] = modiified_phone
print("Phone number is modified to", modiified_phone, "for friend", friend_with_modified_phone)
# if the firend is not present in the dictionary
# inform the user that the friend is not present in the dictionary
# and hence the required operation can not be performed.
else:
print("Friend", friend_with_modified_phone, "is not present in the dictionary and hence can not perform the phone modification operation.")
# When the user chooses an option to
# check if a friend is present in the dictionary or not
elif choice == 'e':
friend_to_be_checked = input("Enter the name of the friend whose presence should be checked in the dictionary : ")
# If the friend is present in the dictionary,
# inform the user that the friend is present in the dictionary.
if friend_with_modified_phone in friends:
print("Friend", friend_to_be_checked, "is present in the dictionary.")
# if the friend is not present in the dictionary,
# inform the user that the friend is not present in the dictionary.
else:
print("Friend", friend_to_be_checked, "is not present in the dictionary.")
# When the user chooses an option to
# display the dictionary in sored order of names
elif choice == 'f':
print("Dictionary in the sorted order of names is printed below")
print("Name\tPhone Number")
for name in sorted(friends.keys()):
print(name, "\t", friends[name])
# if the user has choosen the option to exit, come out of the while loop
# (using the break statement)
elif choice == 'z':
print("Exiting")
break
``````
Output from Run:
``````Enter the number of friends: 3
Enter the name of friend 1 : Babu
Enter the phone number of Babu : 1234567890
Enter the name of friend 2 : Aman
Enter the phone number of Aman : 1234567891
Enter the name of friend 3 : Chintu
Enter the phone number of Aman : 1234567892
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : a
Name    Phone Number
Babu    1234567890
Aman    1234567891
Chintu  1234567892
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : b
Enter the name of new firend: Dinu
Enter the phone number of new firend Dinu : 1234567893
The following is the modified dictionary after adding a new friend
Name    Phone Number
Babu    1234567890
Aman    1234567891
Chintu  1234567892
Dinu    1234567893
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : c
Enter the name of friend to delete: Bintu
Friend Bintu is not found in the dictionary and hence not deleted.
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : c
Enter the name of friend to delete: Chintu
Friend Chintu is deleted from the dictionary.
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : d
Enter the name of the friend whose phone is modified: Chintu
Friend Chintu is not present in the dictionary and hence can not perform the phone modification operation.
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : d
Enter the name of the friend whose phone is modified: Aman
Enter the new phone number for friend Aman : 1234567894
Phone number is modified to 1234567894 for friend Aman
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : e
Enter the name of the friend whose presence should be checked in the dictionary : Chintu
Friend Chintu is not present in the dictionary.
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : e
Enter the name of the friend whose presence should be checked in the dictionary : Dinu
Friend Dinu is present in the dictionary.
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : f
Dictionary in the sorted order of names is printed below
Aman    1234567894
Babu    1234567890
Dinu    1234567893
Choose one of the following options:
a. Display the Name and Phone number for all your firends.
b. Add a new friend and his phone number to the friends' dictionary
c. Delete a particular friend from the dictionary
d. Modify the phone number of an existing friend
e. Check if a friend is present in the dictionary or not
f. Display the dictionary in sorted order of names
z. Exit
Enter your choice from the options above (a-f or z) : z
Exiting
``````
Note: The length of the program might be overwhelming/intimidating for the beginners. So, go through it for each option separately, instead of trying to understand all at once. That’ll make the things easier.