Lists and Mutability

Today, we will discuss the following:

  • Review common and useful list methods that modify lists

  • Discuss mutability and aliasing in Python

List Methods: Modify the List

Unlike integers, strings, floats, which are immutable, lists are a mutable objects and can be changed in place.

This has several implications which we will discuss in this and coming lectures.

Useful methods that do modify the list they are called on:

  • .append()

  • .extend()

  • .insert()

  • .remove()

  • .pop()

  • .sort()

Other ways to modify a list in place:

  • direct assignment to a list element

Let us work through these with examples.

# fresh assignment: creates a new list with the name myList
myList = [1, 2, 3, 4] 
myList
[1, 2, 3, 4]
# changing a list value by direct assignment
myList[1] = 7   
myList
[1, 7, 3, 4]
# appending an item to the end of our list
myList.append(5)  
myList
[1, 7, 3, 4, 5]
# extend method lets you append multiple items
myList.extend([6, 8])  
myList 
[1, 7, 3, 4, 5, 6, 8]
# removes the item at index 3 and returns it
myList.pop(3)
4
myList
[1, 7, 3, 5, 6, 8]
# remove the last item and returns it
myList.pop() 
8
myList
[1, 7, 3, 5, 6]
# insert 11 at index 0, shift everything else over
myList.insert(0, 11) 
myList
[11, 1, 7, 3, 5, 6]
# insert at a seemingly out of range index (!!)
myList.insert(10, 12)  
myList
[11, 1, 7, 3, 5, 6, 12]
# removes first occurrence of 12 from the list
myList.remove(12)   
myList
[11, 1, 7, 3, 5, 6]
# if item does not exist, return a value error
#myList.remove(13) 
# sort the list in ascending order
myList.sort()
myList
[1, 3, 5, 6, 7, 11]
# sorting lists of lists sorts based on first elements
# of inner lists
myList2 = [[6, 4], [3, 2], [1, 6]]
myList2.sort()
myList2
[[1, 6], [3, 2], [6, 4]]

Value vs Identity

  • An objects identity never changes in Python once it has been created. You may think of it as the object’s address in memory

  • On the other hand, the value of some objects can change.

  • Objects whose values can change are called mutable; objects whose values cannot change are called immutable

  • The is operator compares the identity of two objects, and the == operator compares the value (contents) of two objects.

  • The id() function returns an integer (like a memory address) representing its identity.

Ints, Strings, Floats are Immutable

Let’s see how the values and identities change with immutable objects.

num = 5
id(num)
4454562224
# the id changes!
num = num + 1
id(num) 
4454562256
word = "Williams"
college = word
word == college
True
print(id(word), id(college))
4498569072 4498569072
# right now, word and college are the same object
word is college
True
word = "Wellesley"
# after updating word, it's id changes
# this happens because strings are immutable
print(id(word), id(college))
4498569264 4498569072
word is college
False

All Sequence Operations Return New Sequences

For both lists and strings, sequence operations return new sequences and do not change the value of the original sequence.

name = "gryffindor"
id(name)
4498442224
name = name[4:8]
name
'find'
id(name)
4498571184
word = 'Hello World!'
word.lower() # returns new string!
'hello world!'
word # does not change
'Hello World!'
# list example
a = [1, 2, 3]
id(a)
4498442368
a = a[:]
id(a) # slicing a list returns a new list!
4498575488

Lists are Mutable

Let’s see how the identities and values behave when using lists (which are mutable).

myList = [1, 2, 3]
newList = [1, 2, 3]

# list2 is an alias (a second name) for myList
list2 = myList
# same values?  
# yes, both lists contain same items [1, 2, 3]
myList == newList 
True
# same identities? 
# no, they are separate lists in memory
myList is newList 
False
# same values? 
myList == list2
True
# same identities?
myList is list2
True
myList.append(4)
myList
[1, 2, 3, 4]
# has list2 changed?
list2 
[1, 2, 3, 4]
id(myList)
4498444224
myList = myList + [5]
myList
[1, 2, 3, 4, 5]
id(myList)
4498442816
id(list2)
4498444224

Understanding Aliasing

Let us try out some more complicated examples that illustrate how aliasing manifests itself in Python.

nums = [23, 19]
words = ['hello', 'world']
mixed = [12, nums, 'nice', words]
words.append('sky')
mixed
[12, [23, 19], 'nice', ['hello', 'world', 'sky']]
mixed[1].append(27)
nums
[23, 19, 27]
mixed
[12, [23, 19, 27], 'nice', ['hello', 'world', 'sky']]