-: Welcome back.
Let's talk about some useful tricks
with lists that you'll see a lot in Python code.
Well, one we have already seen, it's this idea of length.
You're gonna be using this a lot.
That is to figure out the length of a list
which in our case is now seven.
Another really useful trick is to reverse a list.
Now, I've already showed you how to reverse a list
but sometimes you can do that with list slicing.
Again, something that we've seen in string slicing
so I can actually do basket
and then semicolon, semicolon, negative one.
If I click run here, is that what you expected?
Let's go through this line by line.
I've sorted the basket so that it's from A all the way to Z
and then I've reversed this, so it's in Z all the way to A
and then I've reversed the basket again, so that instead
of being in reverse, it's now in order A, B, C, D, E, X.
And this list slicing creates a new list, right?
If I print
basket here
you'll see that the basket is the reversed version
the last action that we've taken on it
versus list slicing that creates a new version.
So this notation, you'll see a lot
with lists when you need to reverse a list
or maybe you need to make a copy of the list like this
or maybe you need a portion of the list.
Another useful thing that you'll see a lot of
is something called range.
Let's comment this out for now.
If I wanted to generate a list really quickly, let's say
from one to a hundred, I can do something like this.
I can say range, and then start, and then stop.
So, let's do a hundred,
and if I print this
and I run, hmm,
I get a range one to a hundred.
But if I wrap this in a list, which is going to
create a new list for us, and I click run,
look at that.
I get a range from one all the way to 99.
Remember, it's the start and then whatever to stop at.
So before it hits a hundred, it's going to stop.
Now, if I remove this and just do one single thing
I get a list starting from zero all the way to 99.
If I want a list that has 101 items, because it starts
at zero, I would just do 101.
And range is something we'll talk about a lot more
especially when we talk about loops in programming.
But this is a great way of you to generate a list really
really quickly, especially a numbered list.
Finally, the last common thing that you'll see a lot
is this idea of dot join.
What is dot join?
Join is actually something that works on strings.
It's a string method, but it's often used this way.
We have an empty string and this empty string
let's say sentence, and then we can do sentence dot, join
and join takes what we call an iterable,
which is a list for now.
And in here we can have a sentence like,
"Hi, my name is Jojo."
And now if I print the sentence
and I click run here, hmm, I don't get anything.
Let's figure out why.
What if I type in an exclamation mark here and I click run?
All right, it's printing out sentence, but it looks
like dot join creates a new item for us.
So let's just assign that to new sentence.
Now, if I do new sentence and print that out
we see that I get, Hi, my name is Jojo.
Hmm. So this is a little funky, right?
We have these exclamation marks between our items
and join joins these little iterable items
these lists by whatever is in front of it.
So if we actually change this
to an empty string, click run, we now get this.
If I add a space in between and click run,
we get, Hi, my name is Jojo.
And sometimes you'll notice this pattern a lot
especially if you wanna join
and create a new string out of a list of items.
Now, a shorthand way
of doing this is to actually not even bother with this
and just doing an empty string dot join
and then joining all the list items.
There we go.
So all we are doing is combining lists into a string
and this is a common pattern that you'll see a lot of.
I'll see in the next one.
Bye-bye.