Repository navigation
exercise4/answer.py: does insert(3, 'pink') survive future edits, and is the tuple unpack intended?
#16
|
In colors = ["red", "green","blue"]
colors.append("vintage")
colors.insert(3, "pink")so "pink" lands at index 3, right before "vintage" — but that position is hardcoded against the current list shape. If a future edit adds another append above this line, Second thing in the same file: point = ("x=10", "y=20", "z=30")
x, y, z = pointUnpacking gives |
Replies: 1 comment
|
Both flags are worth raising, and they're separable: 1. colors.insert(colors.index("vintage"), "pink") # "before vintage", by name
colors.insert(-1, "pink") # "before the last element"With the current sequence both are equivalent to 2. The tuple unpack — intended as practice, but there's a real bug in the payload. Look at the elements: point = ("x=10", "y=20", "z=30")
x, y, z = pointThose are strings, not numbers — the quotes make point = (10, 20, 30)
x, y, z = pointThe |
Both flags are worth raising, and they're separable:
1.
insert(3, 'pink')against a hardcoded shape — yes, fragile. The intent (from the output order) is "pink lands right before vintage." Index 3 only happens to encode that because the list is 4 long at that moment. Intent-preserving alternatives:With the current sequence both are equivalent to
insert(3, ...)— but they survive future appends above, which is exactly your concern.insert(-1, ...)is the smallest change;index("vintage")is the most self-documenting.2. The tuple unpack …