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 Tuple Immutability

00:00 What is immutability? The English language definition of immutability is not subject to change. In programming, it’s much the same. An object is considered immutable if it can’t be changed after creation. In Python, the str and tuple types would be examples of immutable types. Even methods that appear to change a tuple or a str will in fact return a copy and not tamper with the original object.

00:25 In contrast, objects that can be altered after creation are called mutable. Examples of mutable objects in Python would be the list and dict types.

00:34 So for instance, when you append to a list, you’re actually changing the underlying object. This is an important distinction because mutable and immutable objects are often handled differently.

00:46 There are some important characteristics of immutable objects in Python that you should know. They’re hashable, meaning an immutable object can be passed as the argument to a hash function.

00:56 Hash functions are very important in programming. They take in an object as input and return a unique number called a hash code. Immutable objects in Python can be used as dictionary keys and set values.

01:09 This is because under the hood, both of those data structures use hash codes to look up items. Finally, immutable objects have a fixed state, and this allows the Python interpreter to optimize their handling in memory.

01:23 Alright, you know what’s next: time to hit the REPL for some examples. And guess who it is? It’s Jane, our tuple with four elements.

01:36 Now, what if you try to change Jane? Right now, Jane’s fourth element is the string “Canada.” What if Jane moves to the United States? Can you change the value at index three?

01:48 You can try to assign the value at index three something else, like the string “United States”, but what happens is you get a TypeError because tuples do not support item assignment.

01:59 Immutability in action.

02:02 Now, suppose Jane decides to escape, goes on the run and decides to live as a woman with no name. Her name, Jane Doe, is stored in the tuple at index zero.

02:13 Can you use the del keyword to delete that element? del Jane[0]. Nope, again, you get a TypeError. Sorry, Jane. No anonymity for you.

02:26 But there is a caveat when it comes to immutability. Because tuples can store objects of any type, they can also store mutable objects. For example, create a tuple where one of the elements is a list.

02:46 Here you have student_info, the tuple, where the third item is itself a list containing three strings: “Math,” “Physics,” and “History.” Now because lists are mutable, you can access an index in the list and overwrite it.

03:01 Go ahead and access student_info[2][2] and assign it the string “Computer Science.”

03:12 Now, if you take a look at student_info, you can see the third item in the list at the third position of student_info, while it originally had the value “History,” it now has the value of “Computer Science.” So you see the list changes despite the tuple’s fundamental immutability.

03:30 To get real technical, the reason for this is because the tuple holds only a reference to the memory location of that list object. And while the list mutates, its location in memory stays the same, meaning that the tuple also stays the same despite the appearance of having changed. And now, an important warning: All items in a tuple must be immutable for the tuple itself to be hashable and therefore be a valid dictionary key.

04:00 To see this, go ahead and create a dictionary with tuples for keys.

04:12 student_courses is a dictionary where the keys represent first name, last name string tuples of student names, and the values are lists of student courses.

04:22 You can access a list of Jane Doe’s courses using the string tuple of her name,

04:34 the result: “English” and “History.” However, if you were to construct the tuple keys for the dictionary using mutable values inside the tuple, such as a list?

04:50 The first element in each of the tuple keys is a list of names instead of an immutable string.

04:57 So what happens next? You will encounter a TypeError as lists cannot be hashed.

05:04 Wow, a lot of info sure was packed into this lesson. And speaking of packing, next up you’ll learn all about tuple packing and unpacking.

Become a Member to join the conversation.