← Lesson
Copy vs Change in Place
Joeven
Run
Reset
Python loads on first run
import copy transcript = ["hello"] shared = transcript shared.append("oops") print("shared list", transcript) print("same object?", id(transcript) == id(shared)) a = {"msgs": ["hi"]} shallow = copy.copy(a) shallow["msgs"].append("there") print("after shallow copy, original", a) b = {"msgs": ["hi"]} deep = copy.deepcopy(b) deep["msgs"].append("there") print("after deep copy, original", b) agent1 = ["user: hi"] agent2 = agent1 agent2.append("assistant: hello") print("agent1 saw agent2", agent1) own1 = ["user: hi"] own2 = list(own1) own2.append("assistant: hello") print("own1", own1) print("own2", own2) print("own lists same object?", id(own1) == id(own2)) rows = [{"content": "hi"}] shallow_rows = list(rows) shallow_rows[0]["content"] = "edited" print("original row after shallow list copy", rows[0]["content"])
Run to execute this in your browser. Nothing is sent to a server.