Worked Examples · Example 9.3
Q.Example 9.3: Copy a list using slicing (Method 1, newList = oldList[:]) — create a copy of list1 = [1,2,3,4,5] with list2 = list1[:].
Tamil Nadu DgeTextbookSubjective· 2mImportance★★★★★est
19% · 5/26 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 →list2 = list1[:] takes a slice of the entire list, which produces a brand-new list object with the same elements — an independent copy, unlike list2 = list1 which only copies the reference.
The idea (copying via slicing). A slice list1[start:end] always constructs a new list. Omitting both bounds (list1[:]) means "from the beginning to the end", so the new list contains every element of the original. Since it is a different object in memory, later changes to one list do not affect the other. Contrast this with list2 = list1, which makes both names point to the same object.
Program
list1 = [1, 2, 3, 4, 5]
list2 = list1[:] # Method 1: copy using slicing
print("list1:", list1)
print("list2:", list2)
print("Same object?", list2 is list1)
# prove the copy is independent
list2[0] = 100
print("After list2[0] = 100")
print("list1:", list1)
print("list2:", list2)
Expected output
list1: [1, 2, 3, 4, 5]
list2: [1, 2, 3, 4, 5]
Same object? False
After list2[0] = 100
list1: [1, 2, 3, 4, 5]
list2: [100, 2, 3, 4, 5]
Key points
list1[:]is a slice covering all indices, so a full copy is returned. …
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.