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 can be mutated via both names.

Consider this

Think of a famous person. What aliases do they have, or what other names or nicknames do they go by?

Answer: Bill Gates

Nicknames: Bill, William, William Gates, William Henry Gates III

28.1. Using object aliases

28.2. Making copies of mutable objects

Summary