Solved

Why does append inside a function change my list?

JackStackOP

The function received the list as a parameter. I thought it got a copy.

2

Comments 4

Join the discussion

Already have an account? Log in

  1. MateoG Solutionreplies: 2

    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.

    11
    1. JackStackOPreplies: 1

      So parameters are just more names for the same object. Same lesson as backup = crew.

      0
      1. AmeliaRuns

        Clean output, untouched raw data. Exactly how data pipelines should work.

        2
    2. FreyaL

      Stage 3 without_spikes — return a filtered copy, and the stored list stays with the 9999.0 spikes, as the expected output shows.

      1