List Basics

Let's create a list that has three items. Lists are a kind of 'collection' such that they collect or contain things (a.ka.a 'members' or 'items'. So, in the list, things, we have a collection of 3 chars.


In [8]:
things = [100, 500, "thing"]

To see what the 'your_list' variable contains...


In [2]:
things


Out[2]:
[100, 500, 'things']

We can confirm that this is a list by using 'type'


In [3]:
type(things)


Out[3]:
list

Like strings, lists have a length


In [4]:
len(things)


Out[4]:
3

Also like strings, list have content that can be checked. By using the 'in' operator, we can find out if a char such as the letter 'c' is contained in the list, your_list


In [5]:
"e" in "Hello"


Out[5]:
True

In [10]:
"thing" in things


Out[10]:
True

In [9]:
"nothing" in things


Out[9]:
False

In [12]:
things = [100, 500, "things"]

List Indexes

Lists are ordered. We can access any individual item in a list based on its position inside the list. Let's say I wanted to get the leftmost item.


In [13]:
things[0]


Out[13]:
100

Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1.


In [22]:
things[1]


Out[22]:
500

In [23]:
things[2]


Out[23]:
'things'

What happens if we try to access item 3?


In [16]:
things[3]


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-605fe438a752> in <module>()
----> 1 things[3]

IndexError: list index out of range

We get a traceback, but a pretty readable one.

Fortunately, we can add items to the end of the list


In [24]:
things.append("d")

In [25]:
len(things)


Out[25]:
5

In [26]:
things[3]


Out[26]:
'd'

Adding stuff to lists

What if we want to append a bunch of stuff? A related question is how do we even know we can append to this list? We don't rely on memorization, we use documentation. Let's type python lists into Google.

We'll see the "extend" function which looks like what we need to add a bunch of items to the list at once.

Another way of using documentation is by the built-in 'help' function. It's not pretty, and it's not as intuitive, but it works. To exit out of help, you have to press "q".

In general, if you want to know whether it's possible to do something in Python, Google it.

Here's a question: how would you create an empty list?


In [27]:
stuff = []

Now let's check the length!


In [28]:
len(stuff)


Out[28]:
0

I have no stuff. :(

Let's add some things to my list of stuff


In [29]:
stuff.append("laptop")

In [30]:
stuff


Out[30]:
['laptop']

In [32]:
stuff.append("wedding ring")

In [33]:
stuff


Out[33]:
['laptop', 'wedding ring', 'wedding ring']

Okay now we want to replace laptop with Macbook Pro.


In [35]:
stuff[0] = "Macobook Pro"

In [36]:
stuff


Out[36]:
['Macobook Pro', 'wedding ring', 'wedding ring']

Now let's replace wedding ring with tungsten wedding ring


In [37]:
stuff[2] = "tungsten wedding ring"

Here's a question: how is this different from the line above?


In [38]:
stuff[2] == "tungsten wedding ring"


Out[38]:
True

How do we get the last item in the list?

We know we can get the length of the list


In [39]:
len(stuff)


Out[39]:
3

To get the last item's index, let's try using the length of the list, stuff, minus one.


In [40]:
len(stuff) - 1


Out[40]:
2

In [41]:
stuff[len(stuff) - 1]


Out[41]:
'tungsten wedding ring'

But there's a shortcut in Python for getting the last item from a list.

You can use negative numbers to start counting from the end of the list. So -1 gives us the last item:


In [42]:
stuff[-1]


Out[42]:
'tungsten wedding ring'

It's just doing the math for us. There's no real magic here.

We can do the same for strings to access a character or the last character within a string, its length, and so on.


In [43]:
name = "Danny"

In [44]:
name[0]


Out[44]:
'D'

In [45]:
name[-1]


Out[45]:
'y'

A quick review of lists: So the way we create a list is we give it a name and in square brackets give it a comma-separated set of items.


In [53]:
fruits = ["apples", "bananas", "oranges"]

We can use this square brackets syntax to get individual items


In [47]:
fruits[0]


Out[47]:
'apples'

We can use negative numbers to get items from the end of a list


In [48]:
fruits[-1]


Out[48]:
'oranges'

We can do asignment. We can replace items in the list by using the notation to get the item and then do an assignment.


In [49]:
fruits[0] = "plums"

We can add to the end of the list with .append


In [50]:
fruits.append("cherries")

We can check the length of a list


In [51]:
len(fruits)


Out[51]:
4

We can do things like check for things contained in a list using the 'in' operator


In [52]:
"apples" in fruits


Out[52]:
False

Question: can we grab more than one item from a list?

This is the range syntax:


In [54]:
fruits = ["apples", "bananas", "oranges"]

Slicing of lists


In [55]:
fruits[0:2]


Out[55]:
['apples', 'bananas']

The left side of the colon is implicit. It starts at the beginning by default, but does not include the last item in the list.


In [56]:
fruits[:2]


Out[56]:
['apples', 'bananas']

The right side of the colon can also be left off. It starts from and ends at the end of the list


In [57]:
fruits[2:]


Out[57]:
['oranges']

By extension, if you leave off both the left and right sides you can make a copy of a list


In [58]:
fruits[:]


Out[58]:
['apples', 'bananas', 'oranges']

In [59]:
my_favorite_fruits = fruits[:]

Now we see that the original fruits list


In [60]:
fruits


Out[60]:
['apples', 'bananas', 'oranges']

has indeed been copied into my_favorite_fruits list


In [61]:
my_favorite_fruits


Out[61]:
['apples', 'bananas', 'oranges']

In [62]:
names = ["Danny", "Audrey", "Risa", "Alain"]

Loops

What if we wanted to loop over and do some action for every item in the list?


In [ ]:
print("Hello")
print("My name is Danny")
print("I live in San Diego")

In [ ]:
print("Hello")
print("My name is Audrey")
print("I live in San Diego")

In [ ]:
print("Hello")
print("My name is Risa")
print("I live in San Diego")

That's the hard way. The easy way is by using a 'for' loop. A simple one:


In [63]:
for name in names:
    print(name)


Danny
Audrey
Risa
Alain

To hammer home that this is a variable name of our choosing let's use a different variable name, x.


In [64]:
for x in names:
    print(x)


Danny
Audrey
Risa
Alain

We can have arbitrarily complex statements and as many lines as we want as long as we keep it indented


In [85]:
for name in names:
    print("Hello", name)


Hello Danny
Hello Audrey
Hello Risa
Hello Alain

What if we only want to print out the names that start with a vowel?

How do we check whether a name starts with a vowel?

We know how to get the first character


In [66]:
name = "Audrey"

In [67]:
name[0]


Out[67]:
'A'

We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this


In [68]:
name[0] == "A" or name[0] == "E" or name[0] == "I" or name[0] == "O" or name[0] == "U"


Out[68]:
True

We could also say this:


In [69]:
name[0] in "AEIOU"


Out[69]:
True

We could even say:


In [70]:
name[0] in ["A", "E", "I", "O", "U"]


Out[70]:
True

Now let's setup our for loop


In [71]:
for name in names:
    if name[0] in "AEIOU":
        print(name + " starts with a vowel")


Audrey starts with a vowel
Alain starts with a vowel

The amount of work we had to do here is independent of the length of the list. This list could have had a billion elements in it and this code would still work.

Instead of printing the names, how do we build up a list of only the names that start with a vowel?

We're going to need a for loop. We're also going to need some storage; so let's use a list as storage.


In [72]:
vowel_names = []

In [73]:
for name in names:
    if name[0] in "AEIOU":
        vowel_names.append(name)

Nothing is printed. Let's check that vowel_names has the vowel names


In [74]:
vowel_names


Out[74]:
['Audrey', 'Alain']

Let's say that I'm going to the store and I bought goods and I have their costs. What if I want to add up all the items?


In [75]:
prices = [1.5, 2.35, 5.99, 16.49]

It's sort of like we have a running total. I need to setup some storage ahead of time again.


In [76]:
total = 0

In [77]:
for cost in prices:
    total = total + cost  # looks funny but when you think about it, it makes sense

In [78]:
total


Out[78]:
26.33

Now I'm cheating on you guys a little bit because there's a function called sum that does the same thing so we'd probably use that in an actual program


In [ ]:
sum(prices)

In general, be comfortable with not knowing things. Looking things up is important when programming.

Looping over lists of lists

Remember that earlier example?


In [80]:
print("Hello")
print("My name is Danny")
print("I live in San Diego")


Hello
My name is Danny
I live in California

What if I didn't live in San Diego and Alain does? How would we I resolve that with a list and a loop?

The answer is to use a list of lists.


In [88]:
mentors = [["Danny", "Inland Empire"], ["Audrey", "Corona"], ["Alain", "San Diego"]]
print(mentors)


Out[88]:
[['Danny', 'Inland Empire'], ['Audrey', 'Corona'], ['Alain', 'San Diego']]

okay, let's loop through them.


In [89]:
for mentor in mentors:
    print(mentor)


['Danny', 'Inland Empire']
['Audrey', 'Corona']
['Alain', 'San Diego']

We can loop through the big list, then use indexes to grab stuff from the internal list


In [91]:
for mentor in mentors:  # Don't forget to end the statement with a :
    print("Hello")
    print("My name is", mentor[0])
    print("I live in", mentor[1])


Hello
My name is Danny
I live in Inland Empire
Hello
My name is Audrey
I live in Corona
Hello
My name is Alain
I live in San Diego

Neato Trick


In [92]:
for name, city in mentors:  # Don't forget to end the statement with a :
    print("Hello")
    print("My name is", name)
    print("I live in", city)


Hello
My name is Danny
I live in Inland Empire
Hello
My name is Audrey
I live in Corona
Hello
My name is Alain
I live in San Diego

You can also do this without the use of a loop


In [94]:
danny, audrey, alain = mentors
print(danny)


Out[94]:
['Danny', 'Inland Empire']

Sets

A set is similar to a list, except it eats the duplicates.

Let's take a list of things where we want to remove duplicates. Why? Well, what if we want to count the number of different, distinct words used in the Gettysburg Address? Google for it (it's on wikipedia and stick it into a string.


In [98]:
gettysburg_address = """Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal."""

We'll use the split() method of strings to chop that up into words.


In [99]:
words = gettysburg_address.split(" ")
print(words)


['Four', 'score', 'and', 'seven', 'years', 'ago', 'our', 'fathers', 'brought', 'forth', 'on', 'this', 'continent', 'a', 'new', 'nation,', 'conceived', 'in', 'liberty,', 'and', 'dedicated', 'to', 'the', 'proposition', 'that', 'all', 'men', 'are', 'created', 'equal.']

Not perfect, but lets count the words it generates using the len() method


In [100]:
print(len(words))


30

If you look at the address however, you'll see that 'and' is used several times. Let's turn our list into a set. Using.... well.... the set method.


In [102]:
wordset = set(words)
print(wordset)


{'fathers', 'proposition', 'forth', 'equal.', 'conceived', 'men', 'nation,', 'ago', 'created', 'in', 'a', 'that', 'dedicated', 'are', 'our', 'brought', 'the', 'seven', 'and', 'score', 'all', 'this', 'Four', 'new', 'continent', 'on', 'liberty,', 'to', 'years'}

Whoa! Looks different! Curly braces instead of bracket.s Maybe even shorter! Let's check, shall we?


In [103]:
print(len(wordset))


29

As you can see, sets removed duplicates.

Extra Credit / Pop Quiz time!

  1. Using the help() method, add the following phrase to the Gettysburg Address wordset: "Abraham Lincoln
  1. sort the list of names in reverse order.