Q.What will be the output of the following code segment:
a.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)
b.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
c.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
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 →del list[slice] deletes every element the slice would select: [3:] cuts the tail leaving [1, 2, 3], [:5] cuts the head leaving [6, 7, 8, 9, 10], and [::2] strips the even-indexed elements leaving [2, 4, 6, 8, 10].
The idea. The del statement removes elements in place. Combined with a slice it removes the whole selection at once — so the trick is simply to work out which indices the slice covers, remembering slices are start-inclusive, stop-exclusive, and [::2] means "every second index starting from 0".
a.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)
myList[3:] selects indices 3…9 (values 4-10); deleting them keeps only indices 0-2.
[1, 2, 3]
b.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
myList[:5] selects indices 0…4 (values 1-5); after deletion the remaining elements shift to the front.
[6, 7, 8, 9, 10]
c.
myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
myList[::2] selects indices 0, 2, 4, 6, 8 — the values 1, 3, 5, 7, 9:
| Index selected by [::2] | Value removed |
|---|---|
| 0 | 1 |
| 2 | 3 |
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.