Worked Examples · Example 9.4
Q.Example 9.4: Copy a list using the built-in function list() (Method 2, newList = list(oldList)) — create a copy of list1 = [10,20,30,40] with list2 = list(list1).
Tripura TbseTextbookSubjective· 2mImportance★★★★★est
27% · 7/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 →list(oldList) calls the built-in list constructor on an existing list and returns a new list object with the same elements — copy Method 2, equivalent in effect to slicing with [:].
The idea (copying via the list() constructor). list(iterable) always builds a fresh list by walking through the iterable you give it. Passing an existing list therefore yields a new list object whose elements are the same — a genuine copy, not a second name for the same object.
Program
list1 = [10, 20, 30, 40]
list2 = list(list1) # Method 2: copy using the built-in list()
print("list1:", list1)
print("list2:", list2)
print("Same object?", list2 is list1)
# prove independence
list2.append(50)
print("After list2.append(50)")
print("list1:", list1)
print("list2:", list2)
Expected output
list1: [10, 20, 30, 40]
list2: [10, 20, 30, 40]
Same object? False
After list2.append(50)
list1: [10, 20, 30, 40]
list2: [10, 20, 30, 40, 50]
Key points
list(list1)iterates overlist1and fills a brand-new list — solist2 is list1isFalse.- Because the objects are distinct,
list2.append(50)grows only the copy. …
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.