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 against
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 an 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.
ABC 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 the 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 a hundred 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 1 all the way to ninety nine.
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 ninety
nine.
If I want a list that has a hundred and one items because it starts at zero I would just do one hundred
and one end 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 a sentence.
Dot join and join takes what we call an automobile which is a list for now.
And in here we can have a sentence like high My name is
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 Doug join creates a new item for us.
So let's just sign that to the new sentence.
Now if I do new sentence and print that out
we see that I get Hi my name is Jo Jo.
So this is a little funky right.
We have these exclamation marks between our items and join joins these little interval 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 joint and then joining all the list items.
There we go.
So all we're doing is combining lists into a strength.
And this is a common pattern that you'll see a lot of I'll see in the next one by.