Ass 2
Ass 2
LISTS
YASH PARASHAR XI-G
Question:1
The record of a student [Name, Roll No, Marks in five subjects and
percentage of marks) is stored in the following list
stRecord=[‘Raman’,’A-36’,[56,98,99,72,69],78.8]
Write Python program to retrieve the following information from
the list stRecord.
a. Percentage of the student
b. Marks in the fifth subject
c. Maximum marks of the student
d. Roll number of the student
e. Change the name of the student from ‘Raman’ to ‘Raghav’
Input:
stRecord = ['Raman', 'A-36', [56, 98, 99, 72, 69], 78.8]
# Retrieve the required information
percentage = stRecord[3]
fifth_subject_marks = stRecord[2][4]
max_marks = max(stRecord[2])
roll_number = stRecord[1]
# Change the name from 'Raman' to 'Raghav'
stRecord[0] = 'Raghav'
# Display the results
print("Percentage of the student:", percentage)
print("Marks in the fifth subject:", fifth_subject_marks)
print("Maximum marks of the student:", max_marks)
print("Roll number of the student:", roll_number)
print("Updated student record:", stRecord)
Output:
Percentage of the student: 78.8
Marks in the fifth subject: 69
Maximum marks of the student: 99
Roll number of the student: A-36
Updated student record: ['Raghav', 'A-36', [56, 98, 99, 72, 69], 78.8]
Question:2
Write a program to find the number of times an element occurs in the
list.
Input:
# List of elements
my_list = [1, 2, 3, 4, 2, 5, 2, 6, 7, 2, 8]
# Element to count
element = int(input("Enter the element to count: "))
# Count occurrences
count = my_list.count(element)
# Display the result
print("The element", element, "occurs", count, "times in the list.")
Output:
Question:3
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 the three
lists.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of integers
numbers = []
for i in range(n):
num = int(input("Enter number: "))
numbers.append(num)
# Separate positive and negative numbers
positive_list = []
negative_list = []
for num in numbers:
if num >= 0:
positive_list.append(num)
else:
negative_list.append(num)
# Print the lists
print("Original list:", numbers)
print("Positive numbers list:", positive_list)
print("Negative numbers list:", negative_list)
Output:
Enter the number of elements: 5
Enter number: -3
Enter number: 7
Enter number: -1
Enter number: 4
Enter number: 0
Original list: [-3, 7, -1, 4, 0]
Positive numbers list: [7, 4, 0]
Negative numbers list: [-3, -1]
Question:4
Write a program to find the largest element of the list.
Input:
# Input a list from the user
my_list = input("Enter elements of the list separated by space: ").split()
# Convert the input strings to integers
for i in range(len(my_list)):
my_list[i] = int(my_list[i])
# Find the largest element in the list
largest_element = max(my_list)
# Display the largest element
print("The largest element in the list is:", largest_element)
Output:
Enter elements of the list separated by space: 1 2 3 4 5 6
The largest element in the list is: 6
Question:5
Write a program to find the second largest number from a list of
numbers.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of numbers
numbers = []
for i in range(n):
num = int(input("Enter number: "))
numbers.append(num)
# Find the second largest number
if len(set(numbers)) < 2:
print("No second largest number, all elements are the same.")
else:
numbers.sort(reverse=True) # Sort in descending order
second_largest = numbers[1] # Second element is the second largest
print("Second largest number:", second_largest)
Output:
Enter the number of elements: 5
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: 40
Enter number: 50
Second largest number: 40
Question:6
Write a program to read a list of n integers and find their median.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of integers
numbers = []
for i in range(n):
num = int(input("Enter number: "))
numbers.append(num)
# Sort the list
numbers.sort()
# Find the median
if n % 2 == 1:
median = numbers[n // 2] # Middle element for odd count
else:
median = (numbers[n // 2 - 1] + numbers[n // 2]) / 2 # Average of two middle
elements
# Print the median
print("Median:", median)
Output:
Enter the number of elements: 5
Enter number: 7
Enter number: 2
Enter number: 9
Enter number: 3
Enter number: 6
Median: 6
Question:7
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 once.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of elements
elements = []
for i in range(n):
num = int(input("Enter number: "))
elements.append(num)
# Remove duplicates by converting the list to a set and back to a list
unique_elements = list(set(elements))
# Print the modified list
print("List after removing duplicates:", unique_elements)
Output:
Enter the number of elements: 6
Enter number: 10
Enter number: 20
Enter number: 10
Enter number: 30
Enter number: 20
Enter number: 40
List after removing duplicates: [40, 10, 20, 30]
Question:8
Write a program to read elements of a list.
• 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.
• 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.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of elements
elements = []
for i in range(n):
num = int(input("Enter number: "))
elements.append(num)
# Display the original list
print("Original list:", elements)
# Delete by position
pos = int(input("Enter the position to delete (0-based index): "))
if 0 <= pos < len(elements):
del elements[pos]
else:
print("Invalid position!")
# Display the updated list
print("List after deleting by position:", elements)
# Delete by value
value = int(input("Enter the value to delete: "))
if value in elements:
elements.remove(value)
else:
print("Value not found in the list!")
# Display the final list
print("Final list after deleting by value:", elements)
Output:
Enter the number of elements: 5
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: 40
Enter number: 50
Original list: [10, 20, 30, 40, 50]
Enter the position to delete (0-based index): 2
List after deleting by position: [10, 20, 40, 50]
Enter the value to delete: 40
Final list after deleting by value: [10, 20, 50]
Question:9
Read a list of n elements. Reverse this list in-place without creating a
new list.
Input:
# Read the number of elements
n = int(input("Enter the number of elements: "))
# Read the list of elements
elements = []
for i in range(n):
num = int(input("Enter number: "))
elements.append(num)
# Reverse the list in-place
elements.reverse() # In-place reversal
# Print the reversed list
print("Reversed list:", elements)
Output:
Enter the number of elements: 5
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: 40
Enter number: 50
Reversed list: [50, 40, 30, 20, 10]
Question:10
Write a menu driven program to allow the user to perform any of the
list operation given in a menu as follows:
• Append an element
• Insert an element
• Append a list to the given list
• Modify an existing element
• Delete an existing element from its position
• Delete an existing element from its position
• Delete an existing element with a given value
• Sort the list in ascending order
• Sort the list in descending order
• Display the list
Input:
# Initialize an empty list
elements = []
# Menu-driven loop
while True:
# Display the menu
print("\nMenu:")
print("1. Append an element")
print("2. Insert an element")
print("3. Append a list to the given list")
print("4. Modify an existing element")
print("5. Delete an existing element from its position")
print("6. Delete an existing element by value")
print("7. Sort the list in ascending order")
print("8. Sort the list in descending order")
print("9. Display the list")
print("0. Exit")
# Take user input for the operation
choice = int(input("Enter your choice: "))
if choice == 1:
# Append an element
num = int(input("Enter the element to append: "))
elements.append(num)
elif choice == 2:
# Insert an element
num = int(input("Enter the element to insert: "))
pos = int(input("Enter the position to insert the element (0-based index): "))
if 0 <= pos <= len(elements):
elements.insert(pos, num)
else:
print("Invalid position!")
elif choice == 3:
# Append a list to the given list
new_list = list(map(int, input("Enter elements of the list to append (space-
separated): ").split()))
elements.extend(new_list)
elif choice == 4:
# Modify an existing element
pos = int(input("Enter the position of the element to modify (0-based index): "))
if 0 <= pos < len(elements):
new_value = int(input("Enter the new value: "))
elements[pos] = new_value
else:
print("Invalid position!")
elif choice == 5:
# Delete an existing element from its position
pos = int(input("Enter the position of the element to delete (0-based index): "))
if 0 <= pos < len(elements):
del elements[pos]
else:
print("Invalid position!")
elif choice == 6:
# Delete an existing element by value
value = int(input("Enter the value to delete: "))
if value in elements:
elements.remove(value)
else:
print("Value not found in the list!")
elif choice == 7:
# Sort the list in ascending order
elements.sort()
elif choice == 8:
# Sort the list in descending order
elements.sort(reverse=True)
elif choice == 9:
# Display the list
print("Current List:", elements)
elif choice == 0:
# Exit the program
print("Exiting...")
break
else:
print("Invalid choice, please try again.")
Output:
Menu:
1. Append an element
2. Insert an element
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element from its position
6. Delete an existing element by value
7. Sort the list in ascending order
8. Sort the list in descending order
9. Display the list
0. Exit
Enter your choice: 1
Enter the element to append: 10
Menu:
1. Append an element
2. Insert an element
3. Append a list to the given list
4. Modify an existing element
5. Delete an existing element from its position
6. Delete an existing element by value
7. Sort the list in ascending order
8. Sort the list in descending order
9. Display the list
0. Exit
Enter your choice: 9
Current List: [10]