1 00:00:00,740 --> 00:00:06,710 Often you will want to start with an existing list and make an entirely new list based on the first 2 00:00:06,710 --> 00:00:06,980 one. 3 00:00:06,980 --> 00:00:14,370 So let's explore how copying a list works and examine one situation in which copying a list is useful. 4 00:00:14,390 --> 00:00:21,080 So to copy a list, you can make a slice that includes the entire original list by omitting the first 5 00:00:21,080 --> 00:00:24,290 index and the second index like this. 6 00:00:25,410 --> 00:00:25,930 Right. 7 00:00:26,170 --> 00:00:33,340 So this tells Python to make a slice that starts at the first item and ends with the last item producing 8 00:00:33,340 --> 00:00:35,110 a copy of the entire list. 9 00:00:35,230 --> 00:00:42,550 For example, imagine we have a list of four list of our favorite car makers and want to make a separate 10 00:00:42,550 --> 00:00:47,470 list of car makers that our friend drives. 11 00:00:47,470 --> 00:00:53,710 So this friend likes everything in our list and drives everything on our list so far so we can create 12 00:00:53,740 --> 00:00:56,660 their list by copying ours. 13 00:00:56,690 --> 00:01:03,160 So in order to do that, you will write friend's friend that. 14 00:01:05,610 --> 00:01:07,140 Friend that. 15 00:01:08,150 --> 00:01:10,310 Likes cars. 16 00:01:10,310 --> 00:01:14,030 And here we will use this and let's actually my. 17 00:01:15,280 --> 00:01:17,140 My favorite. 18 00:01:19,710 --> 00:01:21,150 Custom cars. 19 00:01:21,150 --> 00:01:22,380 And here. 20 00:01:23,280 --> 00:01:24,060 Friends. 21 00:01:29,940 --> 00:01:31,200 And here. 22 00:01:31,890 --> 00:01:33,480 Now we will use this. 23 00:01:33,510 --> 00:01:35,310 My favorite cars. 24 00:01:35,580 --> 00:01:36,570 And this. 25 00:01:37,760 --> 00:01:39,050 And after that we will. 26 00:01:39,050 --> 00:01:39,890 Uh, firstly. 27 00:01:40,860 --> 00:01:45,090 Prentiss Brown's favorite cars and after that. 28 00:01:45,090 --> 00:01:50,790 So firstly, of course, this is the friend favorite cars and this is my favorite cars. 29 00:01:50,790 --> 00:01:55,380 And after that we first we will first print the my favorite cars. 30 00:01:55,380 --> 00:01:56,790 So print. 31 00:01:58,000 --> 00:01:59,920 My favorite. 32 00:02:02,030 --> 00:02:04,580 Cars are like this. 33 00:02:04,580 --> 00:02:07,670 And after that we will print our my favourite cars. 34 00:02:07,760 --> 00:02:09,740 And after that we will print. 35 00:02:09,770 --> 00:02:12,140 Of course we need to add new line before it. 36 00:02:12,200 --> 00:02:14,750 So my friends. 37 00:02:17,650 --> 00:02:21,220 Favorite cars are here. 38 00:02:21,340 --> 00:02:22,960 And after that we will print. 39 00:02:23,450 --> 00:02:24,430 Friends. 40 00:02:25,000 --> 00:02:25,900 Friends. 41 00:02:25,900 --> 00:02:26,950 Favorite cars. 42 00:02:27,870 --> 00:02:30,480 And here we will run this now. 43 00:02:30,480 --> 00:02:32,480 And as you can see, this is our output. 44 00:02:32,490 --> 00:02:35,760 So I added this new line here. 45 00:02:35,790 --> 00:02:37,440 Oops, sorry again. 46 00:02:41,290 --> 00:02:42,700 It's not working or. 47 00:02:42,700 --> 00:02:44,320 Yes, it's working now. 48 00:02:44,350 --> 00:02:44,830 Okay. 49 00:02:45,930 --> 00:02:52,140 So here I added this new line character to this to show our output. 50 00:02:52,170 --> 00:02:53,040 New line here. 51 00:02:53,040 --> 00:02:56,010 So I could we added this new line after the. 52 00:02:58,400 --> 00:03:01,310 My favorite cars here, but it's more easy to add here. 53 00:03:01,310 --> 00:03:07,430 Instead of writing some formatting and things in last my favorite cars print function. 54 00:03:07,760 --> 00:03:18,950 So first we make a list of car makers we we like called or favorite car makers called my favorite cars. 55 00:03:18,950 --> 00:03:24,470 Then we make a new list called Friends, Friends, Favorite Cars. 56 00:03:24,830 --> 00:03:34,880 And we make a copy of my favorite cars by asking for a slice of my favorite cars without specifying 57 00:03:34,880 --> 00:03:36,950 an indicator like this here. 58 00:03:37,840 --> 00:03:43,060 And assign the copy of my favorite cars to. 59 00:03:43,890 --> 00:03:45,000 Friend's favorite cars. 60 00:03:45,000 --> 00:03:52,800 So when and when we print each list, we see that they both contain the same cars. 61 00:03:52,800 --> 00:03:57,090 So to prove that we actually have two separate lists, we will add a new. 62 00:03:57,830 --> 00:03:59,380 Uh, we will add a new. 63 00:04:02,200 --> 00:04:09,640 Favor of a new car to each list and show that we that each list keeps track of the appropriate person's 64 00:04:09,640 --> 00:04:10,710 favorite car. 65 00:04:10,720 --> 00:04:15,880 So in order to do that, we will do after my favorite cars here we will do. 66 00:04:18,700 --> 00:04:19,540 My. 67 00:04:21,120 --> 00:04:24,510 My favorite cars that append here. 68 00:04:24,540 --> 00:04:29,550 Let's add new car maker name in this case, Toyota. 69 00:04:29,550 --> 00:04:30,010 Right. 70 00:04:30,030 --> 00:04:31,920 So Toyota. 71 00:04:33,500 --> 00:04:33,620 Of. 72 00:04:35,100 --> 00:04:35,970 Toyota here. 73 00:04:35,970 --> 00:04:42,690 And after that, we will also add friends favorite cars and we will append the. 74 00:04:44,230 --> 00:04:44,990 No, that's sad. 75 00:04:49,500 --> 00:04:50,430 Fiat, right? 76 00:04:51,500 --> 00:04:52,700 And here. 77 00:04:53,630 --> 00:04:59,690 We will not change this print functions and now we will run our code and see what will change here. 78 00:04:59,690 --> 00:05:07,640 And as you can see here, so we copy the original items in my favorite cars to the new list called Friends 79 00:05:07,640 --> 00:05:10,460 Favorite Cars, as we did in the previous example. 80 00:05:10,460 --> 00:05:19,550 And next we add a new favorite new car maker to each list and we add Toyota to my favorite cars and 81 00:05:19,550 --> 00:05:22,940 we add fiat to friends, favorite cars. 82 00:05:22,940 --> 00:05:31,210 And when and we then print the to list to see whether their whether each of these favorite cars in the 83 00:05:31,250 --> 00:05:32,660 is in the appropriate list. 84 00:05:32,660 --> 00:05:36,020 So here the output shows that our. 85 00:05:37,140 --> 00:05:40,710 Toyota is appears in our list of favorite cars. 86 00:05:40,710 --> 00:05:43,320 But Fiat here. 87 00:05:43,350 --> 00:05:45,450 Fiat does not. 88 00:05:45,450 --> 00:05:51,390 So we can see that Fiat now appears in our friends list. 89 00:05:51,780 --> 00:05:53,780 But Toyota doesn't write. 90 00:05:53,790 --> 00:06:01,980 If you had a simple set friend, favorite friends, favorite cars equal to my favorite cars, we will 91 00:06:01,980 --> 00:06:03,330 not produce two separate lists. 92 00:06:03,330 --> 00:06:11,490 For example, here's what happens when you try to copy a list without using a slice. 93 00:06:17,560 --> 00:06:18,940 Without using a slice. 94 00:06:19,180 --> 00:06:22,000 And as you can see here, we both have. 95 00:06:22,570 --> 00:06:30,940 So instead of assigning a copy of my foods to my my favorite cars, to my friend's favorite cars, we 96 00:06:30,940 --> 00:06:34,690 set a friend's favorite cars equal to my favorite cars. 97 00:06:34,690 --> 00:06:41,380 So this syntax actually tells Python to associate the new variable to my favorite cars with a list that 98 00:06:41,380 --> 00:06:45,400 is already associated with my favorite cars. 99 00:06:45,400 --> 00:06:47,830 So now my favorite cars. 100 00:06:47,830 --> 00:06:52,150 It will also appear in friends, favorite cars and so on. 101 00:06:52,150 --> 00:06:59,770 So likewise, the Toyota will appear in both lists, even though or Fiat will appear in both lists, 102 00:06:59,770 --> 00:07:03,280 even though it appears to be added only to my favorite cars. 103 00:07:03,280 --> 00:07:08,110 And as you can see here, Toyota and Fiat appears in the same list. 104 00:07:08,110 --> 00:07:16,270 So the output shows that both lists are the same now, which is not we wanted here and don't worry about 105 00:07:16,270 --> 00:07:17,600 the details in this example. 106 00:07:17,600 --> 00:07:23,360 For now, if you are trying to work with the with a copy of list and you see an expected behavior, 107 00:07:23,360 --> 00:07:30,080 make sure you are copying the list using a slice as we did in this in the first example here.