Danger Zone: Mixing Mutable and Immutable Data Structures
In this lesson, you’ll see that using a combination of mutable and immutable data structures can still lead to problems. Even if you use an immutable data structure like
namedtuple, you still run the risk of modifying your data set if you store your immutable data structures in mutables ones, like lists.
In the next lesson, you’ll see how to address this problem by replacing the rest of your mutables data structures with immutable ones.
Now, we’ve got the
scientists list, and while I can’t really go in and do this, what I did earlier—I guess now we’d have to restructure it a little bit because now this isn’t a key, this is actually a field. I can’t do that anymore, but what I can still do is I can do this and delete the first scientist,
01:11 right? And now poor Emmy is all by herself, and that’s a problem because when you look at what I did there, well, I was trying to define this immutable data structure, but what I actually did is I defined these immutable items and then stored them all in a list, which is a mutable data structure in Python.
Become a Member to join the conversation.