Mutable Data Structures: Lists and Dictionaries
In this lesson, you’ll see how you could approach this data set using mutable data structures, like lists and dictionaries. When you use mutable data structures, their contents can be modified. So if you use them to hold your data, then you run the risk of messing up your data set, since it can be changed.
If you want to have a multithreaded program and do parallel processing, then using immutable data structures would allow you to not have to worry about locking the data structures because there would be no way to update them.
When I started thinking about this tutorial, my first hunch was, “Okay, if I want to bring this into Python, I’d probably have a list of dictionaries.” So I could say, okay,
scientists = and then I would just have a list of dictionary objects—I should probably indent that—and I would put different keys, like
00:49 And you can imagine that there would be some more here, so let me just copy and paste that. This is a little bit quicker. There will be more data here. And eventually, we close that list. Now, we have this data inside Python and we can work with that. This looks kind of nice, right?
I mean that I can just reach in here and I can say, “Okay, we’re going to grab the first scientist here.” I’m just going to reach inside this data structure, and I’m going to rename Ada, give her a different name, right? And now when I print this out, we actually modified the
'name' of the scientist and it kind of screws up the data. In the beginning, I said, you know, one of the core tenants of functional programming is actually that you mainly work—or ideally, always work with immutable data structure.
02:11 You want a data structure that can’t be modified that way because if you represent your data using immutable data structures, it has some really interesting properties. For example, if you wanted to have a multithreaded program—you want to do some parallel processing there—you wouldn’t have to worry about locking the data structure because there was no way to update it. You know, all of these reads can happen in parallel, and we’d never have to worry about changing the state of this data structure while another thread is accessing it.
02:45 to if you use immutable data structures that cannot be modified, like this—So, this example here, this was a mutable data structure that I could modify at will, you know, at any time. With an immutable data structure, I couldn’t do that.
02:58 And that, a lot of times, leads to a cleaner conceptual model. I’m forced to do my calculations in a different way and it’s very easy, also, to come up with a history of the calculations that I do, you know, because I can’t just reach in and modify this existing object, but instead what I’d have to do is I’d have to create a full copy of this data structure that I can modify.
03:22 And so now, there exist two copies that I can use in some kind of history, and I could trace back, you know, that I made these changes, here. And of course, all of that takes up more memory, at least if you do it in Python. In other programming languages, they have better immutable data structures.
Become a Member to join the conversation.