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.
things = [100, 500, "thing"]
To see what the 'your_list' variable contains...
things
[100, 500, 'things']
We can confirm that this is a list by using 'type'
type(things)
list
Like strings, lists have a length
len(things)
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
"e" in "Hello"
True
"thing" in things
True
"nothing" in things
False
things = [100, 500, "things"]
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.
things[0]
100
Notice that we start counting at zero, which is a little funny since one might think it would start with the number, 1.
things[1]
500
things[2]
'things'
What happens if we try to access item 3?
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
things.append("d")
len(things)
5
things[3]
'd'
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?
stuff = []
Now let's check the length!
len(stuff)
0
I have no stuff. :(
Let's add some things to my list of stuff
stuff.append("laptop")
stuff
['laptop']
stuff.append("wedding ring")
stuff
['laptop', 'wedding ring', 'wedding ring']
Okay now we want to replace laptop with Macbook Pro.
stuff[0] = "Macobook Pro"
stuff
['Macobook Pro', 'wedding ring', 'wedding ring']
Now let's replace wedding ring with tungsten wedding ring
stuff[2] = "tungsten wedding ring"
Here's a question: how is this different from the line above?
stuff[2] == "tungsten wedding ring"
True
How do we get the last item in the list?
We know we can get the length of the list
len(stuff)
3
To get the last item's index, let's try using the length of the list, stuff, minus one.
len(stuff) - 1
2
stuff[len(stuff) - 1]
'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:
stuff[-1]
'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.
name = "Danny"
name[0]
'D'
name[-1]
'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.
fruits = ["apples", "bananas", "oranges"]
We can use this square brackets syntax to get individual items
fruits[0]
'apples'
We can use negative numbers to get items from the end of a list
fruits[-1]
'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.
fruits[0] = "plums"
We can add to the end of the list with .append
fruits.append("cherries")
We can check the length of a list
len(fruits)
4
We can do things like check for things contained in a list using the 'in' operator
"apples" in fruits
False
Question: can we grab more than one item from a list?
This is the range syntax:
fruits = ["apples", "bananas", "oranges"]
fruits[0:2]
['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.
fruits[:2]
['apples', 'bananas']
The right side of the colon can also be left off. It starts from and ends at the end of the list
fruits[2:]
['oranges']
By extension, if you leave off both the left and right sides you can make a copy of a list
fruits[:]
['apples', 'bananas', 'oranges']
my_favorite_fruits = fruits[:]
Now we see that the original fruits list
fruits
['apples', 'bananas', 'oranges']
has indeed been copied into my_favorite_fruits list
my_favorite_fruits
['apples', 'bananas', 'oranges']
names = ["Danny", "Audrey", "Risa", "Alain"]
What if we wanted to loop over and do some action for every item in the list?
print("Hello")
print("My name is Danny")
print("I live in San Diego")
print("Hello")
print("My name is Audrey")
print("I live in San Diego")
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:
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.
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
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
name = "Audrey"
name[0]
'A'
We want to check whether it equals A, E, I, O, or U. There are a lot of different ways to write this
name[0] == "A" or name[0] == "E" or name[0] == "I" or name[0] == "O" or name[0] == "U"
True
We could also say this:
name[0] in "AEIOU"
True
We could even say:
name[0] in ["A", "E", "I", "O", "U"]
True
Now let's setup our for loop
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.
vowel_names = []
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
vowel_names
['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?
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.
total = 0
for cost in prices:
total = total + cost # looks funny but when you think about it, it makes sense
total
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
sum(prices)
In general, be comfortable with not knowing things. Looking things up is important when programming.
Remember that earlier example?
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.
mentors = [["Danny", "Inland Empire"], ["Audrey", "Corona"], ["Alain", "San Diego"]]
print(mentors)
[['Danny', 'Inland Empire'], ['Audrey', 'Corona'], ['Alain', 'San Diego']]
okay, let's loop through them.
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
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
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
danny, audrey, alain = mentors
print(danny)
['Danny', 'Inland Empire']
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.
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.
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
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.
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?
print(len(wordset))
29
As you can see, sets removed duplicates.