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 going to 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 1 to 100, I can do something like this.
I can say range and then start and then stop.
So let's do 100.
And if I print this.
And I run.
I get a range 1 to 100.
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 it.
So before it hits 100, 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 joint join.
What is dot joint 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 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 again.
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 want to 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're 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.