Udacity CS 101: Lists Part I ([], Mutation, Alias)

We have learned about strings, and how strings are basically sequences of characters. In this new unit, Professor Evans introduces us to a new kind of data structure, called list. Lists are much more accommodating, they are sequences of anything. Here is an example of a list assignment:

stringSay = 'Hello!'

listSay = ['H','e','l','l','o','!']

Like how strings are recognized by the quotes, lists are identified by square brackets, and the elements are separated by commas. Remember how we could use square brackets to call out a particular character or a sequence of characters in the string, we can do that to lists too!

stringSay[0] # refers to the character 'H'

listSay[0] # refers to the character 'H'

stringSay[1:3] # refers to the string 'el'

listSay[1:3] # refers to a list ['e','l']

Now to the coolest part of lists, list can contain anything! Yes really anything: lists can contain numbers, strings, characters and even lists!

friends = [['Adam',21],['Ben', 18],['Carl',12],['Edward',16]]

So above is a list of friends that I have, and each element in the list is a list containing the name and age of my friends. I can call out the elements by using square brackets:

print friends[1] # will print ['Ben', 18]

This will call out the first element of the friends list, which is a list. If we want to get elements in a list, in the friends list, we can use two brackets, this calls the second element of the second element of friends list.

print friends[1][1] # will print 18

The next thing we need to know about lists are that they can be mutated, in other words you can change the contents of the list (compare that to strings):

stringSay = 'Hello!'

stringSay = 'Bye!' # this creates a new string 'Bye!', and stringSay now refers to the new string

listSay = ['H','e','l','l','o','!']

listSay[0] = 'B' # this will change the value of listSay[0], 'H', to 'B'

print listSay # will give you ['B','e','l','l','o','!']

Another thing about lists mutation is that if you assign the same list to another variable, manipulating the new variable will affect the original list. Lets see an example below:

listSay = ['H','e','l','l','o','!']

listGreet = listSay

listSay[0] = 'B'

print listGreet # will give you ['B','e','l','l','o','!']

When we have different ways of referring to the same object, we call it aliasing

Aliasing

1 person, many identities.
Image courtesy of fanpop.com

So now you have listSay and listGreet both refering to the same object, and modifying an element through either list will modify that object. But if you do something like this:

listSay = ['B','Y','E','!']

print listSay # will give you ['B','Y','E','!']

print listGreet # will give you ['B','e','l','l','o','!']

Essentially you have pointed listSay to a new list, and listGreet still references the same old list. Now listSay and listGreet are no longer alias.

This Udacity post references Udacity CS 101 Unit 3 Chapters 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 and 13.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s