Q.Consider a list:
list1 = [6,7,8,9]
What is the difference between the following operations on list1:
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 →All three produce the elements [6, 7, 8, 9, 6, 7, 8, 9], but they differ in what happens to list1: (a) is a bare expression — list1 is unchanged;
(b) *= doubles the same list object in place;
(c) = binds the name to a brand-new doubled list. The id() of the object tells them apart.
Starting each time from list1 = [6, 7, 8, 9]:
a) list1 * 2 — an expression, nothing stored
list1 = [6, 7, 8, 9]
print(list1 * 2) # the computed value
print(list1) # the original, untouched
[6, 7, 8, 9, 6, 7, 8, 9]
[6, 7, 8, 9]
* on a list builds a new repeated list as a value. Written on its own line without assignment, that value is discarded — list1 still has 4 elements.
b) list1 *= 2 — in-place repetition, same object
list1 = [6, 7, 8, 9]
before = id(list1)
list1 *= 2
print(list1)
print(id(list1) == before)
[6, 7, 8, 9, 6, 7, 8, 9]
True
The augmented assignment *= modifies the existing list object in place (like extend-ing it with a copy of itself). The id() check proves it is still the same object — so any other variable pointing to this list sees the change too.
c) list1 = list1 * 2 — a new object rebound to the name
list1 = [6, 7, 8, 9]
before = id(list1)
list1 = list1 * 2
print(list1)
print(id(list1) == before)
[6, 7, 8, 9, 6, 7, 8, 9]
False
Here the right side first builds a new doubled list, and the assignment then points the name list1 at it. The original 4-element object is abandoned (and garbage-collected if nothing else references it). If another variable, say alias = list1, had been made before this line, alias would still show the old 4-element list — the visible difference from (b).
Summary table …
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.