Lesson 28. Aliasing and copying lists and dictionaries

After reading lesson 28, you’ll be able to

  • Make aliases for mutable objects (lists and dictionaries)
  • Make copies of mutable objects (lists and dictionaries)
  • Make sorted copies of lists
  • Remove elements from mutable objects based on certain criteria

Mutable objects are great to use because they allow you to modify the object itself without making a copy. When your mutable objects are large, this behavior makes sense because otherwise, making a copy of a large item every time you make a change to it is expensive and wasteful. But using mutable objects introduces a side effect that you need to be aware of: you can have more than one variable bound to the same mutable object, and the object ...

Get Get Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.