Often you will want to start with an existing list and make an entirely new list based on the first
one.
So let's explore how copying a list works and examine one situation in which copying a list is useful.
So to copy a list, you can make a slice that includes the entire original list by omitting the first
index and the second index like this.
Right.
So this tells Python to make a slice that starts at the first item and ends with the last item producing
a copy of the entire list.
For example, imagine we have a list of four list of our favorite car makers and want to make a separate
list of car makers that our friend drives.
So this friend likes everything in our list and drives everything on our list so far so we can create
their list by copying ours.
So in order to do that, you will write friend's friend that.
Friend that.
Likes cars.
And here we will use this and let's actually my.
My favorite.
Custom cars.
And here.
Friends.
And here.
Now we will use this.
My favorite cars.
And this.
And after that we will.
Uh, firstly.
Prentiss Brown's favorite cars and after that.
So firstly, of course, this is the friend favorite cars and this is my favorite cars.
And after that we first we will first print the my favorite cars.
So print.
My favorite.
Cars are like this.
And after that we will print our my favourite cars.
And after that we will print.
Of course we need to add new line before it.
So my friends.
Favorite cars are here.
And after that we will print.
Friends.
Favorite cars.
And here we will run this now.
And as you can see, this is our output.
So I added this new line here.
Oops, sorry again.
It's not working or.
Yes, it's working now.
Okay.
So here I added this new line character to this to show our output.
New line here.
So I could we added this new line after the.
My favorite cars here, but it's more easy to add here.
Instead of writing some formatting and things in last my favorite cars print function.
So first we make a list of car makers we we like called or favorite car makers called my favorite cars.
Then we make a new list called Friends, Friends, Favorite Cars.
And we make a copy of my favorite cars by asking for a slice of my favorite cars without specifying
an indicator like this here.
And assign the copy of my favorite cars to.
Friend's favorite cars.
So when and when we print each list, we see that they both contain the same cars.
So to prove that we actually have two separate lists, we will add a new.
Uh, we will add a new.
Favor of a new car to each list and show that we that each list keeps track of the appropriate person's
favorite car.
So in order to do that, we will do after my favorite cars here we will do.
My.
My favorite cars that append here.
Let's add new car maker name in this case, Toyota.
Right.
So Toyota.
Of.
Toyota here.
And after that, we will also add friends favorite cars and we will append the.
No, that's sad.
Fiat, right?
And here.
We will not change this print functions and now we will run our code and see what will change here.
And as you can see here, so we copy the original items in my favorite cars to the new list called Friends
Favorite Cars, as we did in the previous example.
And next we add a new favorite new car maker to each list and we add Toyota to my favorite cars and
we add fiat to friends, favorite cars.
And when and we then print the to list to see whether their whether each of these favorite cars in the
is in the appropriate list.
So here the output shows that our.
Toyota is appears in our list of favorite cars.
But Fiat here.
Fiat does not.
So we can see that Fiat now appears in our friends list.
But Toyota doesn't write.
If you had a simple set friend, favorite friends, favorite cars equal to my favorite cars, we will
not produce two separate lists.
For example, here's what happens when you try to copy a list without using a slice.
Without using a slice.
And as you can see here, we both have.
So instead of assigning a copy of my foods to my my favorite cars, to my friend's favorite cars, we
set a friend's favorite cars equal to my favorite cars.
So this syntax actually tells Python to associate the new variable to my favorite cars with a list that
is already associated with my favorite cars.
So now my favorite cars.
It will also appear in friends, favorite cars and so on.
So likewise, the Toyota will appear in both lists, even though or Fiat will appear in both lists,
even though it appears to be added only to my favorite cars.
And as you can see here, Toyota and Fiat appears in the same list.
So the output shows that both lists are the same now, which is not we wanted here and don't worry about
the details in this example.
For now, if you are trying to work with the with a copy of list and you see an expected behavior,
make sure you are copying the list using a slice as we did in this in the first example here.