Programming Problems · Q6
Q.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.
Odisha ChseTextbookSubjective· 3mImportance★★★★★est
87% · 26/30 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →list.insert(position, element) places the element at that index and shifts everything
after it one step right — nothing is overwritten. Read the list, the element and the position,
then one insert() call does the job.
The idea
Unlike append() (which can only add at the end) and index assignment lst[i] = x
(which replaces), insert(i, x) makes room: the element lands at index i and the old
occupants move right.
Program
# Insert an element at a given position in a list
n = int(input("How many elements? "))
lst = []
for i in range(n):
lst.append(int(input("Enter element " + str(i + 1) + ": ")))
element = int(input("Element to insert : "))
position = int(input("Position (index, 0 onwards): "))
lst.insert(position, element)
print("List after insertion:", lst)
Sample run
How many elements? 5
Enter element 1: 10
Enter element 2: 20
Enter element 3: 30
Enter element 4: 40
Enter element 5: 50
Element to insert : 25
Position (index, 0 onwards): 2
List after insertion: [10, 20, 25, 30, 40, 50]
Dry run of lst.insert(2, 25):
| Index | Before | After |
|---|---|---|
| 0 | 10 | 10 |
| 1 | 20 | 20 |
| 2 | 30 | 25 (inserted) |
| 3 | 40 | 30 (shifted right) |
| 4 | 50 | 40 |
| 5 | — | 50 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.