1 00:00:00,025 --> 00:00:04,292 So now that we've seen how to create a list, let's take a look at a powerful and 2 00:00:04,292 --> 00:00:06,990 common feature of lists, adding items. 3 00:00:06,990 --> 00:00:08,870 So, let's go ahead and pop open a REPL. 4 00:00:08,870 --> 00:00:12,481 So I'm gonna open up the Python Shell with Python. 5 00:00:12,481 --> 00:00:17,240 And one thing I should note is that lists can store any object, any data type. 6 00:00:17,240 --> 00:00:21,717 So, let's say I wanted to store my temperature over time. 7 00:00:21,717 --> 00:00:26,420 As I went to the doctor, they can just add to my records, right? 8 00:00:26,420 --> 00:00:29,280 So, let's create an empty list to store these readings. 9 00:00:29,280 --> 00:00:35,934 So we'll say temperatures =, and I'm gonna do an empty list, 10 00:00:35,934 --> 00:00:41,000 and again that's [ ], empty list, awesome. 11 00:00:41,000 --> 00:00:44,234 So now, let's add my temperature to that. 12 00:00:44,234 --> 00:00:47,650 So what we wanna do is, we want to append a value to our list. 13 00:00:47,650 --> 00:00:52,636 So it'll say temperatures and there's a method that allows you to do this. 14 00:00:52,636 --> 00:00:57,700 So I'm going to put a dot, because I wanna get to the methods. 15 00:00:57,700 --> 00:01:00,530 And the method name that allows you to add a value to the end of 16 00:01:00,530 --> 00:01:01,840 the list is called append. 17 00:01:01,840 --> 00:01:03,813 So I'm gonna say append. 18 00:01:03,813 --> 00:01:09,342 And I will pass in the item and the first time I was running (98.6). 19 00:01:09,342 --> 00:01:13,253 Now, don't worry I'm not super sick, those are in Fahrenheit and 20 00:01:13,253 --> 00:01:15,390 that's the standard temperature. 21 00:01:15,390 --> 00:01:18,413 And then the next trip to my doctor they took my temperature, 22 00:01:18,413 --> 00:01:20,110 I was running a little bit hot. 23 00:01:20,110 --> 00:01:21,950 So I'm gonna append another value. 24 00:01:21,950 --> 00:01:29,120 We'll do (99.4), and let's take a look here at our list. 25 00:01:29,120 --> 00:01:31,750 Here we go, we got two lists in there, just keep on appending and 26 00:01:31,750 --> 00:01:34,190 wait until the end of the list, right? 27 00:01:34,190 --> 00:01:38,334 You'll note that when I'm appending these values, I'm not being returned a new list. 28 00:01:38,334 --> 00:01:42,840 I'm actually modifying the existing list in place. 29 00:01:42,840 --> 00:01:46,379 That's because lists are mutable, you're able to change them. 30 00:01:46,379 --> 00:01:48,710 So I end up catching a flu. 31 00:01:48,710 --> 00:01:51,640 And it was so bad that I ended up the emergency room. 32 00:01:51,640 --> 00:01:55,200 And they stored their temperatures there for me. 33 00:01:55,200 --> 00:01:56,827 And this is what it look like in there, it was pretty bad. 34 00:01:56,827 --> 00:01:58,975 So these are my er_temps. 35 00:01:58,975 --> 00:02:00,376 And I'm going to create a new list. 36 00:02:00,376 --> 00:02:06,210 I was at [102.2, that's really high if you don't speak in Fahrenheit. 37 00:02:06,210 --> 00:02:09,970 And then I was 101.1 and started going back down to 99.9]. 38 00:02:09,970 --> 00:02:15,060 So, when I came back to my doctor, I wanted to combine the two lists, right? 39 00:02:15,060 --> 00:02:16,761 It's the same data that we're talking about, 40 00:02:16,761 --> 00:02:18,217 it's just from two different sources. 41 00:02:18,217 --> 00:02:23,662 So, if you wanna add all items from one list onto an existing list, 42 00:02:23,662 --> 00:02:27,200 you can use a method known as Extend. 43 00:02:27,200 --> 00:02:30,380 So I'm gonna use my original list, which was temperatures. 44 00:02:30,380 --> 00:02:36,702 I'm going to extend that with my other value, which is (er_temps). 45 00:02:39,176 --> 00:02:41,402 Now, if I take a look at temperatures, 46 00:02:41,402 --> 00:02:44,920 you'll see that I have both of those lists together, right? 47 00:02:44,920 --> 00:02:49,034 It just appending these all at the end, it extended the list. 48 00:02:49,034 --> 00:02:53,338 Now sometimes, extending a list isn't exactly what you want. 49 00:02:53,338 --> 00:02:56,051 You might wanna have a combination of the two lists, but 50 00:02:56,051 --> 00:02:57,561 not modify either one of them. 51 00:02:57,561 --> 00:03:02,350 You want a brand new list and leave the previous lists alone. 52 00:03:02,350 --> 00:03:04,650 This is where concatenation comes into play. 53 00:03:04,650 --> 00:03:09,681 Concatenation works with the plus sign and it's just like string concatenation. 54 00:03:09,681 --> 00:03:11,170 So here, let's do this. 55 00:03:11,170 --> 00:03:13,310 We'll say that I have some primary_care_doctors. 56 00:03:13,310 --> 00:03:16,610 You might of heard of these doctors. 57 00:03:16,610 --> 00:03:23,520 Go to a doctor called ["Dr Scholls"], and my other go to doctor is "Dr Pepper"]. 58 00:03:23,520 --> 00:03:29,620 And when I was at the I didn't catch their names, all I caught was their first names. 59 00:03:29,620 --> 00:03:32,894 And so, it was ["Doug", and "Susan"], I was super out of it, 60 00:03:32,894 --> 00:03:34,995 they were really super attractive though. 61 00:03:37,850 --> 00:03:41,518 But I don't wanna change my primary care doctors, right? 62 00:03:41,518 --> 00:03:45,003 Suppose I wanted to show a list of everybody who helped me through this 63 00:03:45,003 --> 00:03:45,990 flu season. 64 00:03:45,990 --> 00:03:49,744 What I can do is, I can concatenate those two lists together. 65 00:03:49,744 --> 00:03:52,643 So if I wanted to say all_doctors. 66 00:03:52,643 --> 00:03:57,493 And I could say, I wanted to use my primary_care_doctors and 67 00:03:57,493 --> 00:04:00,000 I wanted to use my er_doctors. 68 00:04:00,000 --> 00:04:03,110 That created a brand new list and stuck it in all_doctors. 69 00:04:03,110 --> 00:04:07,436 So all_doctors is now referring to a list that has both 'Doug' and 'Susan' and 70 00:04:07,436 --> 00:04:12,599 'Dr Scholls' and 'Dr Pepper' together, but it didn't affect my primary_care_doctors. 71 00:04:13,980 --> 00:04:18,828 One more thing that I'd like to make sure we remember is that these lists can store 72 00:04:18,828 --> 00:04:20,264 any data type, right? 73 00:04:20,264 --> 00:04:23,631 So, if I wanted to store in my temperatures, which before we were just 74 00:04:23,631 --> 00:04:26,620 putting floats in there, right, cuz they had that numbers. 75 00:04:26,620 --> 00:04:29,675 If I can append, I can totally append to the (99), it's an integer, but 76 00:04:29,675 --> 00:04:30,524 it would go in there. 77 00:04:30,524 --> 00:04:33,549 And I can even actually, nothing's stopping me from putting in a string, 78 00:04:33,549 --> 00:04:35,290 all right, of like ("Burning up"). 79 00:04:35,290 --> 00:04:40,750 And if I take a look at temperatures, it's totally gonna be fine. 80 00:04:40,750 --> 00:04:44,369 There's an integer and a string in this list, the data type doesn't matter. 81 00:04:44,369 --> 00:04:48,040 Okay, so, back to our meeting example. 82 00:04:48,040 --> 00:04:52,537 What we were trying to do here is, we were trying to get Ashley to come along. 83 00:04:52,537 --> 00:04:55,290 She had pinged me and I wanted to add her to these list. 84 00:04:55,290 --> 00:04:56,560 Now, I could just put her here. 85 00:04:56,560 --> 00:04:59,880 Let's practice our skills, let's give them a little stretch. 86 00:04:59,880 --> 00:05:01,430 So let's add her to adhere to this list. 87 00:05:01,430 --> 00:05:03,403 So what do we do to add her? 88 00:05:03,403 --> 00:05:07,915 We want her at the end of the list, so we are just going to append("Ashley"). 89 00:05:09,080 --> 00:05:10,715 There we go. 90 00:05:10,715 --> 00:05:14,465 And as meetings work, Ashley suggested that we should also have Guil and 91 00:05:14,465 --> 00:05:15,515 James come as well. 92 00:05:15,515 --> 00:05:19,725 So, let's extend our list with Ashley's suggestions. 93 00:05:19,725 --> 00:05:25,505 So she suggested that we add ["James", "Guil"], so that's a list in it's own. 94 00:05:25,505 --> 00:05:31,260 And we want to just put them at the end there of our list. 95 00:05:31,260 --> 00:05:35,673 And since this is a list literal, we've created a new list, and 96 00:05:35,673 --> 00:05:40,350 we want to say attendees.extend(["James", "Guil"]). 97 00:05:40,350 --> 00:05:42,627 Now, if we accidentally said append, 98 00:05:42,627 --> 00:05:45,520 it will would actually append a list to our item. 99 00:05:45,520 --> 00:05:49,138 We'll get to that later, but now the list should be extended and 100 00:05:49,138 --> 00:05:51,690 James and Guil should be appended to the end. 101 00:05:51,690 --> 00:05:56,336 Well, you know what, we should probably add some optional invitees, too. 102 00:05:56,336 --> 00:05:58,640 These meetings tend to grow, don't they? 103 00:05:58,640 --> 00:06:01,060 So we'll do optional_invitees. 104 00:06:01,060 --> 00:06:05,874 And these two are super busy, so I'm gonna not suggest that they come, 105 00:06:05,874 --> 00:06:08,190 but they can come if they want to. 106 00:06:08,190 --> 00:06:13,590 So we got Ben Jacobin and David Farland, so they're optional invitees here. 107 00:06:13,590 --> 00:06:17,494 So, you know what now though, our account's gonna be off here, right? 108 00:06:17,494 --> 00:06:21,220 Cuz this is only looking at the attendees but we might need a bigger room. 109 00:06:22,290 --> 00:06:25,650 So, let's make sure that we have a large enough meeting space. 110 00:06:25,650 --> 00:06:28,350 Let's concatenate these two lists. 111 00:06:28,350 --> 00:06:31,640 So we'll say potential_attendees. 112 00:06:31,640 --> 00:06:36,633 And we don't want to modify either the optional or the original attendees, 113 00:06:36,633 --> 00:06:38,592 we just wanna make a new list. 114 00:06:38,592 --> 00:06:45,094 So we'll say attendees + optional_attendees, and this should be 115 00:06:45,094 --> 00:06:50,020 everybody that could potentially be there and that we will have a large enough room. 116 00:06:50,020 --> 00:06:54,823 So let's make sure that this says there 117 00:06:54,823 --> 00:06:59,774 are a length of potential_attendees, 118 00:06:59,774 --> 00:07:03,570 say there are potential here. 119 00:07:03,570 --> 00:07:06,550 We don't need this space because it's built-in for us. 120 00:07:06,550 --> 00:07:07,915 There we go. 121 00:07:07,915 --> 00:07:10,220 And let's give that a run. 122 00:07:10,220 --> 00:07:16,410 I'm gonna drop out of here, and I will type python meeting.py. 123 00:07:16,410 --> 00:07:19,031 And I spelled something wrong, it looks like, right? 124 00:07:19,031 --> 00:07:21,026 So NameError: optional_attendees. 125 00:07:21,026 --> 00:07:22,650 I said invitees. 126 00:07:22,650 --> 00:07:27,410 So this is optional_invitees. 127 00:07:30,320 --> 00:07:34,770 There are 8 potential attendees currently, awesome. 128 00:07:34,770 --> 00:07:38,253 Now, the order of this list that we're working with here doesn't really matter. 129 00:07:38,253 --> 00:07:42,720 But as you can imagine, there are some list where the order matters. 130 00:07:42,720 --> 00:07:45,465 And you might not want just to add the end of your list, 131 00:07:45,465 --> 00:07:47,980 you might want to insert at a specific location. 132 00:07:47,980 --> 00:07:50,850 Let's take a look at that problem right after this quick break.