Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
A shallow copy creates a new outer container that still references the same inner objects; a deep copy recursively copies the inner objects too, so nothing is shared.
b = a only creates another name for the same object. list(a), a.copy(), a[:] and copy.copy(a) create a shallow copy: a new outer list whose elements are the same objects as before, so changing a nested list through one copy shows up in the other. copy.deepcopy(a) walks the structure and copies nested objects, handling cycles with a memo dictionary. Deep copies are slower and can copy things you did not intend, such as large shared caches, so it is often better to design data as immutable or copy only the parts you change.