Solved Why does append inside a function change my list? JAJackStackOP 30 Sep, 23:35 The function received the list as a parameter. I thought it got a copy.
MAMateoG1 Oct, 07:53 Solutionreplies: 2Python passes a reference to the same object. append() modifies that object in place, so the caller sees the change. To avoid it, build a new list inside the function (e.g. readings + [new]) and return it.
JAJackStackOP1 Oct, 11:08replies: 1So parameters are just more names for the same object. Same lesson as backup = crew.
FRFreyaL1 Oct, 12:18Stage 3 without_spikes — return a filtered copy, and the stored list stays with the 9999.0 spikes, as the expected output shows.
Comments 4
Join the discussion CommentAlready have an account? Log in
Python passes a reference to the same object.
append()modifies that object in place, so the caller sees the change. To avoid it, build a new list inside the function (e.g.readings + [new]) and return it.So parameters are just more names for the same object. Same lesson as backup = crew.
Clean output, untouched raw data. Exactly how data pipelines should work.
Stage 3
without_spikes— return a filtered copy, and the stored list stays with the 9999.0 spikes, as the expected output shows.