Aliasing and copying dictionaries

18-minute lesson · module-dictionaries-03

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Aliasing and copying dictionaries

18-minute lesson · module-dictionaries-03

  • Explain that assigning a dictionary to a new name creates an alias, not a copy.
  • Predict when a mutation through one name is visible through another.
  • Use `.copy()` to make an independent (shallow) copy.
  • Recognize that a shallow copy still shares nested mutable values.

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Dictionaries / Aliasing and copying

Adaptation notes. Reading rewritten in our voice. Examples use an Iowa State student-record theme (a dict of advising notes shared between two variables). Hidden tests check that the student can distinguish aliasing from copying by reporting whether a mutation is shared.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.