Welcome to Chapter 10.
We’re going to talk about tuples, and in a way, you kind of already know tuples.
Tuples are a limited version of lists, and the sum total of this is that,
tuples are sort of a more efficient version of lists that you can’t modify.
So they’re really unmodifiable lists.
So here’s just the syntax of tuples, but it exactly looks like lists,
except we’re going to use parentheses.
So here is a three tuple.
The concept of tuple, the word tuple comes from mathematics.
2 tuples, 3 tuples.
A 2 tuple is a thing that has two things.
It’s a way to call it so 3 tuples.
It’s a set of three things basically.
So this is a 3 tuple with three strings in it.
Again, it looks exactly like a list, except using parentheses.
We use the index operator x sub 2.
We can use the constant syntax.
We assign these things in,
and the printout just prints it out the constant syntax.
And it just has no square braces, but instead parentheses.
Things like max, use tuples.
It looks through a sequence of things.
Lists is a sequence, tuples are a sequence, a string is a sequence.
And so max looks through them, and the for loop basically creates the duration
variable and then bounces through the things that are in a tuple.
And so in a way these are the same, right?
Now there are differences.
The place where there are differences are that tuples are not changeable.
Not, they’re immutable.
And if you recall strings are also, not mutable.
And so if we take a look, if we have a list, 9,8,7, and we can change x
sub 2 to 6, and then that changes the 7 to be a 6, lists are mutable.
Here we have three character string ABC, and we want to do character 2 and
turn that to a D, with this assignment statement.
And Python says, no, we’re not allowed to do that, that is a traceback.
And the same thing happens with a tuple where we want to do z sub 2=0.
And it says sorry, you cannot change.
So these are not mutable.
So the difference is lists are mutable, and then strings and
tuples are not mutable.
But that’s part of the efficiency of tuples, is that they’re not mutable, and
that allows them to be stored more densely than lists.
And so, there’s a whole bunch of things that can’t be done with tuples.
You can’t sort a tuple.
Whatever order you put the tuple in when you create it, it stays in that you can’t
append, you can’t extend it, you can’t flip it with reverse.
Lots of things don’t work.
And if you take a look at the directory output,
of lists and tuples, you see, whoa, it’s a subset.
You can do count and index.
Count looks up how many things match a particular value.
Index says where is a particular value?
But you can’t do, append or extend or insert, pop, remove, because all those
methods inside lists, that tuples don’t have are the ones that make changes to it.
So it’s prohibited.
But it also, allows Python to be more efficient,
knowing that tuples are not going to be changed.
Lists that have to actually allocate extra memory and
stuff like that to let them be changed.
So that’s the point.
It doesn’t have to build these structures to be changeable, and
so they’re just more efficient and more performant than lists.
If you need a list, use a list.
But if you can get away with a tuple, we tend to use tuples, especially if we’re
going to make a variable for a brief moment, and then throw that variable away.
Now, there’s a couple of cool interesting things that we can do with tuples.
And one is, you can put a tuple on a right hand side of an assignment statement.
Now they both have to be variables in this case.
Now, that’s very different than putting a tuple on the left hand side.
If I put it on the left hand side of x and y, it would be, x is 4 and y is 6, and
that’s a 4,6 tuple.
But if we put it on the right hand side,
it basically expects a tuple on the left hand.
I mean if we put it on the left hand side, it expects a tuple on the right hand side,
and then does a one-to-one correspondence.
So it’s the same as saying, x=4 and y equals quote fred quote.
And so that is sort of a simultaneous assignment statement.
You could write this with x=4, y equals fred, right?
That’d be okay.
But you can do this, we’ll see in a second, and just whatever the order is.
Now this could be an expression or even a function that returns a tuple, and
you can then assign it into two variables at the same time.
Now if you do something like a, b=9, it’s going to blow up on that.
It’s going to be, I’m unhappy about that,
because it expects if there’s a tuple on this side.
There’s got to be a tuple on that side.
And in this case, if I just put nine over here, there is no tuple on that side.
Now, we’ve played with this actually already without even knowing it.
And so, the way this works is, if you remember d items,
the items gives you a list of tuples.
So this dictionary has two things.
Csev maps to 2 and cwen maps to 4.
And so, if we say d items, we say give me the tuples, right?
And so the tuples come back.
So this is a list of tuples.
Now what happens is, that is how the two iteration variable four works.
And this is a list of 2 tuples.
And so what happens is the first goes into the k, and the second goes into the v.
This is like an assignment statement, for this first tuple.
Then it runs the loop, and then the second one gets put in the k and the v.
Then it runs the loop again.
And so that’s kind of how we construct these two iteration variable for
loops using tuples,
because that turns out to be a tuple assignment each time through the for loop.
Now an interesting thing that tuples can do is we can compare them.
Remember we could compare strings.
And so, the way tuples work is it does the comparison,
starting at the leftmost one and compares it.
So if it’s asking the question, is this less than?
Well, 0< 5.
And at that point it does not look beyond, the 0 and the 5.
That’s the most significant element of the tuple.
The most significant digit of a number, if at 199< 201.
because if there’s a 1 there and the 2 there, then the rest of
these numbers really don’t matter, because that is the most significant digit.
So that’s how this works.
It doesn’t look at these.
On the other hand if the first one matches, and it’s saying, is it less than?
Then it has to look at the second one.
It says, 1< 3.
It never looks at this one, and it doesn’t look at that one.
But it gives us back true.
The same is true for strings.
It’s doing string by string comparison.
It compares these two, and they’re the same.
And so it has to look to Sally and Sam, and then looks at Sa.
Well they’re the same, so Sa.
And then at some point goes, there’s the l and that m.
And so, the l is less than m.
And so that’s why this entire thing is true.
Jones, Sally, is less than Jones Sam.
If, on the other hand, the first two don’t match, Jones Adams,
then it never looks at the second piece whatsoever.
It doesn’t bother because the j is greater than the a, so this thing becomes true.
So it’s sort of within strings, it looks the first pair,
and then within that, if they’re not the same, and then it goes to the second pair,
third pair, whatever, and so it’s pretty clever.
So that’s one of the nice things about tuples.
And we’re going to use this in a moment to sort them.
And so you can think of tuples as being sortable, by their first item and
where the first item matches, then it looks at the second item.
So if we go back to looking at the items, what comes out?
But if we look at the dictionary items, d items,
then we got a tuple, a,10 c, 22, b, 1.
Now what we can do is we can say, hey, we would like to make a sorted copy of this.
And so we pass this into the sorted function, and
we get back a sorted list, but it sorted based on, the first thing.
Because in dictionaries you can’t have two keys,
you can’t have duplicate keys, right?
You can’t have 2 a’s or 2 b’s.
And so it sorts it based on that.
If they matched, it would look at the second one.
But in this particular case, because it’s coming from a dictionary,
these keys are going to be unique.
Because the very fact of constructing the dictionary,
you can’t put the same key in more than once.
You can put the same value in more than once, but
you can’t put the same key in more than once.
And so sorted basically is this function that takes a sequence, and
it sort of works through the function and
gives you back a list that is the sorted version of that.
And so we can write and compose this, in a for loop as follows.
So, sorted d items would give us the items sort of unsorted.
But if we say sorted and pass in d items,
then we have a sequence that is sorted by key order.
And so k and v are going to go through this dictionary in key order,
not value order, key order.
So this is a way to say, I want to loop
through this dictionary, in key order.
And so that’s what’s going to come out.
So it’s going to print out ABC, regardless of the order inside the dictionary.
We sort it before we loop through it.
And so that’s how we’re going to get this thing in key order.
But not value order.
Now, we’re going to quickly get to how do we sort by value, okay?
So that’s how we sort by key.
That’s really easy, but it takes a little bit more work to sort by value.
But we’re going to be using tuples.
So, here’s how we do it.
So, if we think of these we can get key value tuples, and
all we have to do is make value key tuples.
And if we can make a tuple where the value is first and the key is second,
then we’re okay.
So what we’re going to do is we’re going to create a for
loop that’s going to go through these items, in not particularly any order.
In this case, we’re not using sorted here.
So it’s not any order.
So k and v are going to work their way through these key value pairs.
And what we’re going to do is we’re going to make a new tuple, but
we’re going to reverse the order of what we would get from items.
In that the value is going to be the first item or the zero item in the tuple and
k is going to be the second item.
So if we print this out, they’re not particularly sorted at this point, but
we have the values and the keys.
Value key, value key, right?
Value, key, value key.
Now what we can do, is we can say, this now is just a list.
It’s a list of tuples.
And if we tell it to be sorted, then it’s going to compare these things and
then sort.
And when those match, now the values there can be duplicates.
So after the duplicate, there’s a matching value,
then it will look to the key as the next thing to sort based on.
And so we’re going to call sorted again and we’re going to pass in temp,
which is our little list that we just made, of 2 tuples, flipped value key.
And we’re going to give an extra parameter, say, go backwards.
So sort from high to low, reverse equals true, that’s what that does.
You just add that as a parameter.
Reverse equals true to sorted, and then we print this out.
So it’s now 22, 10, 1.
So it’s from big, value, to small, okay?
So that’s how you get a sorted by value instead of by key, okay?
But we have to make a temporary list.
So temp is a list, because we have to append to it.
But each of the things in the list is a tuple.
So we end up with a list of tuple, tuple, tuple.
Okay, so, this again is data structures.
We’re trying to create shapes of data, that help us solve our problem.
So now let’s actually solve a problem, the ten most common words.
In the previous chapter, we did the most common word, but
now we want the ten most common words.
So here’s how we do it.
So we’ve done this before.
We open a file, we make a dictionary.
We loop through all the lines of the file.
We split the words in the file.
So we got, file looks kind of this,right?
We loop through the lines of the file, and then for
each line we loop through the words.
That’s the outer loop and the inner loop.
And then we do this count, get word 0 + 1.
And that creates our little set of our histograms based on each word.
And the histograms get taller.
And then now what we’ve got is at the end here,
right here, we’ve got the completed histogram, in no particular order.
So now what we’re going to do is we are going to extract the data out of that
dictionary, that histogram data out of the dictionary.
And we are going to make a reverse tuple of the value,
keyword tuple instead of a key value.
So we do what we just did on the previous slide.
We write a for loop.
And, I’m doing a little bit differently here.
We’re going through key value, but then I’m going to make a tuple that’s value
key, put it in the variable new tuple.
This is a tuple assignment that’s going in there.
And then I’m going to append that to the list.
And so we end up with this list, just like on the previous page,
a list of tuples in value key order.
Now, I’m okay, let’s sort that backwards.
So this is the value key tuples, go backwards.
And then we reassign this result assorted back into the list variable.
When this is done,
it will be sorted from the highest value down to the lowest value.
And now, we have to do two things.
Look for the top ten, and we have to flip them out when we’re printing.
So this list is now backwards.
It’s v comma k, value comma k, and we’re going to use list slicing.
So we’re saying start at the first one and go up to, but not including ten.
So that’s 0 through 9, which is the first 10.
And, you’ll notice that this val and key are flipped.
And that’s because in it, value is the first piece of each tuple, and
key is the second piece of each tuple.
And so, it’s going to go through successively value key, value key,
value key, value key, value key.
But it’s only going to go through the first 10 because we have limited this with
list slicing.
And then we print it out in the order we want it to see it in the first place.
So we see kind of a flipping of the order, between how we iterate through the data
structure and then how we print this stuff out.
And that gets us back to the key value, which is what we want.
And so that’s quite a sophisticated thing.
We’re using dictionaries, we’re using lists, and we’re using tuples, and
sorting.
And that’s a nice little piece of code.
Again, I encourage you to understand every single line of this code.
So, this next slide is not something I’m insisting that you understand,
but some of you might find this kind of fascinating.
So I can do effectively this bottom part here, not the calculation, but
this bottom part right there.
I can do this all in one line of code, that takes the dictionary and
then ends up with the sorted tuple sorted by value order.
And this is it.
Let you look at it for a second.
It uses a part of python called list comprehension.
And so the tricky bit here, is this syntax in the middle,
and this looks like a list and it is a list.
The difference is instead of expressing the list with appends or
as a constant with commas, we’re expressing the list as an expression.
And I like to read this as right here, just to say the word for all.
Create me tuples that are v comma k.
And you’ll notice this order is I got value key v comma k, tuples 4.
So this is like there’s a little loop that happens here.
It’s going to stamp out a bunch of these little tuples.
Stamp, stamp, stamp, stamp, stamp, stamp, stamp.
And it’s going to stamp enough, to go all the way through for
each kv that’s in c items.
So that’s the key value in this dictionary.
A 10, b 1, c 22.
This is going to run three times.
It’s going to make three tuples, but this list is going to have them flipped.
So this little bit right here, this little bit in
the brackets, is the same as this bit right here.
It is basically the same as doing that,
except it’s expressing it in a more direct but slightly more complex way.
And so this is a list that’s computed on the fly,
it’s a three tuple list, flipped with values and keys.
And then, I pass this in right into sorted, to the sorted function.
So that’s sorted function parentheses.
And, then I take what comes out of sorted,
which is this, and I pass that into print.
And, I forgot to put reverse equals true on here.
So it comes out but not reversed.
It’s value key but not reversed.
So you get the idea.
And so if you’re really, really interested in this,
go read up a bit on list comprehension.
It’s kind of an interesting and powerful way to make even more succinct Python.
But I don’t spend too much time talking about it,
because if you see this version of it and
you feel like that version of it makes a lot more sense to you, stick with it.
If this version makes a lot more sense to you, then use it.
I mean, once you get good at it, both of them sort of equally speak to you.
But it’s probably easier if you’re certainly beginning to use more simpler
primitives.
And here,
once you’re more sophisticated using sort of these more powerful primitives, okay?
So that’s a quick run through tuples,
we talked about how they’re pretty much like lists.
You can’t change them, you can compare them, they’re sortable, and
you can have the two values that get into an assignment statement.
And, we can now sort dictionaries by key and
value by constructing a data structure, ie, a list of flip tuples, and
then sorting that, and we use that to sort of sorted is there for us.
And if we can set the data up, in a way that sorted does what we want, then we get
the data that we want, and we’re really taking advantage of data structures.