

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of aliasing and mutability in Python, focusing on lists. It covers how lists differ from immutable data types, the impact of aliasing on mutable variables, and the use of destructive and non-destructive functions. Additionally, it introduces 2D lists and their application in handling multi-dimensional data.
Typology: Study notes
1 / 33
This page cannot be seen from the preview
Don't miss anything!
15 - 110 – Wednesday 02/
Unlike the previous types we've worked with, the values in a list can be changed directly , without creating a new list that needs to be assigned to the variable. We can change a list by setting a list index to a new value, like how we would set a variable to a value. lst = [ "a", "b", "c" ] lst[1] = "foo" print(lst) # [ "a", "foo", "c" ]
We call data types that can be modified after being assigned mutable. Data types that cannot be modified directly are called immutable. All the other data types we've learned about so far – integers, floats, Booleans, and strings – are immutable. In fact, if we try to set a string index to a new character, we'll get an error. We have to set the entire variable equal to a new value if we want to change the string. s = "abc" s[1] = "z" # TypeError s = s[:1] + "z" + s[2:]
Recall that all the data we work with in a program must eventually be stored in the computer's memory as binary. Data is stored in memory differently based on whether it is mutable or immutable. Let's compare how memory works for strings (immutable) vs. lists (mutable).
When we modify the value of immutable values , such as strings, numbers, and Booleans, Python makes a new value and reassigns the variable to reference the new value. s = "Hello" s = s + " World" t = s s Hello (^) Hello World Hello World t
When we set a variable to a list , Python sets aside a large place in memory for the list. By breaking up that large chunk of memory into parts, Python can assign each value in the list a location, ordered sequentially. x = [1, 2, 3] x 1 2 3
The use of references causes an interesting behavior when we copy a mutable variable x to a new variable y. The reference is copied, not the values. That means the same set of values is used for both variables. This is called aliasing. x = [1, 2, 3] y = x x 1 2 3 y
Two variables won't be aliased just because they contain the same values. They need to refer to the same location in memory to be aliased. In the following example, the lack of a reference copy keeps the list z from being aliased to x and y. x = [1, 2, 3] y = x z = [1, 2, 3] x (^1 2 ) y z 1 2 3
After the following code is executed, what is the value of x? x = [10, 20, 30] y = x x[2] = "dog" y.append("cat")
If you want to check whether two variables are aliased, you can use a built-in function and an operation. The function id(var) takes in a variable and returns the memory ID that Python associates with it. If two mutable variables have the same memory ID, they're aliased. The is operation returns True if two variables have the same ID, and False otherwise. a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) # True print(a is c) # False (^17)
Whenever we want to modify a list (by changing a value, adding a value, or removing a value), we can choose to do so destructively or non-destructively. Destructive approaches change the data values without changing the variable reference. Any aliases of the variable will see the change as well, since they refer to the same list. Non-destructive approaches make a new list, giving it a new reference. This 'breaks' the alias, and doesn't change the previously-aliased variables.
How do we update a value in a list destructively? Use index assignment. lst = [1, 2, 3] lst[1] = "foo" How do we update a value in a list non-destructively? Use variable assignment with list slicing and concatenation. lst = [1, 2, 3] lst = lst[:1] + ["foo"] + lst[2:]