CBSE Class 11 Informatics Practices Question 59 of 80

Lists in Python — Question 22

Back to all questions
22
Question

Question 17

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.

Solution
list1 = eval(input("Enter list: "))

new_element = int(input("Enter the element to be inserted: "))

position = int(input("Enter the position at which the element should be inserted: "))
list1.insert(position, new_element)
print("Updated list:", list1)
Output
Enter list: [23, 6, 7, 8, 9]
Enter the element to be inserted: 15
Enter the position at which the element should be inserted: 3
Updated list: [23, 6, 7, 15, 8, 9]
Answer