Deepcopy vs. Slice: Which one actually protects your data? 🎧 Audio Edition: Prefer to listen? Check out the expanded AI podcast version of this deep dive on YouTube. 📺 Video Edition: Prefer to watch? Check out the 7-minute visual explainer on YouTube. Timothy was pale. He didn't even look up when Margaret walked in with a fresh pot of Earl Grey. "Margaret, I’ve seen a ghost," Timothy whispered. "I was running a simulation for the Chess Club’s upcoming tournament. I made a 'Practice Bracket' so I could test some player movements without touching the 'Official Bracket.' But... when I changed the Practice version, the Official one changed itself." He showed her his code, his hands trembling slightly on the keyboard. # The Official Bracket: A list of teams (nested lists) official_bracket = [["Alex", "Alice"], ["Bob", "Barbara"]]

# Timothy makes a "Practice" copy using a slice practice_bracket = official_bracket[:]

# He swaps a player in the first match of the practice bracket practice_bracket[0][0] = "Timothy"

print(f"Practice: {practice_bracket}") print(f"Official: {official_bracket}")

Output: Practice: [['Timothy', 'Alice'], ['Bob', 'Barbara']] Official: [['Timothy', 'Alice'], ['Bob', 'Barbara']]

"See?" Timothy pointed at the screen. "I never touched official_bracket[0][0]. I only touched the practice copy. But the change followed me. It’s a ghost in the machine." Margaret pulled up a chair. "It’s not a ghost, Timothy. It’s a Shallow Copy. You thought you were photocopying the documents, but you were actually just photocopying a list of addresses." She drew two large envelopes on the whiteboard. "When you did official_bracket[:], Python created a new list—a new outer envelope," Margaret explained. "But inside that official envelope were two smaller envelopes (the matches). Python didn't bother making new versions of those. It just put the address of the original matches into your new practice envelope." "So when I went to the address in the practice envelope