Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Exploring Mutable Built-in Data Types: Dictionaries & Sets

00:00 Another Python built-in data type that’s very common in Python programs is a dictionary, and dictionaries are also mutable. Let’s have a look at this dictionary, an inventory that has three items.

00:13 The first one has the key apple and there are a hundred of them. The second one is orange, there are 80, and the third one is banana and there are 120.

00:22 As we’ve done before, let’s look at the identity of this dictionary.

00:28 And we end up with a number that ends with 7664. We’ll look out whether this number changes or stays the same as we make changes to this dictionary. One change you could make is to replace, for example, we want the number of apples to be equal to some other value.

00:48 120, for example.

00:51 So the inventory now shows that there are 120 apples. How about the identity? Is the same. The dictionary has not changed even though its contents have changed.

01:03 The keys and values in a dictionary are also objects. So you can, for example, get the identity of one of the values. For example, the value associated with the key orange from the dictionary.

01:17 So inventory["orange"] is the integer 80, and therefore this gives you the identity of the integer 80, which is the value for orange.

01:27 If, however, you now decide to change how many oranges there are let’s say you want this to be 200 now. We like oranges, so let’s put lots of oranges. The identity of the value of orange is now different because this is now pointing to the integer 200, which is a different object to the integer 80 you had before.

01:49 However, even though the contents have changed, the identity of the dictionary itself has not changed. 7664—those are the same last four digits you had earlier when you looked at the identity of the dictionary.

02:04 This dictionary is the same object and it remains the same object. Even if you add values or remove values, for example, you want to add grapes, which is currently not one of the keys in the dictionary, at the moment the inventory is apple, orange, banana.

02:22 Let’s say we have 20 grapes, so inventory now has the grapes in there and identity remains the same, the same dictionary, the same object is now larger because it has an extra key-value pair.

02:40 And the final built-in data type you’ll discuss is the set. So let’s create a set called fruits. You still use the curly braces, but there are no colons in this case.

02:51 This is not a dictionary. It’s a set containing three items.

02:56 You can find the identity of the set, and that’s a number ending in 6688. The number itself doesn’t matter, but we’ll compare it with whatever happens next.

03:06 Let’s call the .add() method

03:10 and let’s add lemons to the set.

03:14 Lemon is included and the identity remains the same. Sets are also mutable objects. Once you create a set, the object remains the same one even if you make changes to the set, and you can try removing one of the items.

03:31 So let’s remove apple, for example,

03:36 and the set no longer contains apple,

03:39 but the identity remains the same. As with all mutable objects, once you create the object, as long as the object stays in memory, it’ll always remain the same object.

03:50 You can add, remove, make changes to it, a mutable type can change without having to create a new object. The object is then cleared once all references to that object are removed.

04:02 So for example, this set only has one reference, the name fruits. So if you delete the variable name fruits, the objects, the sets that had lemon, banana, and orange in it, no longer has any references to it.

04:18 So Python will clear it from memory and therefore this object will no longer exist once Python’s garbage collection removes it from memory.

04:29 So let’s summarize Python’s built-in mutable data types, and these include lists and dictionaries. These are two of the most common data types in Python.

04:39 Sets are also a built-in mutable data type. And incidentally, there’s also an immutable version of the set called the frozenset, which we won’t be talking about in this course.

04:49 And one other mutable built-in data type is the bytearray. This is the mutable version of the bytes data type. If you want to read more about these additional data types, you can do so in the accompanying article, “Python’s Mutable vs Immutable Types: What’s the Difference?”

Become a Member to join the conversation.