Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
What good are lists if you can't add to them? Let's learn how to append, extend and concatenate.
Additional thoughts
Remember String
s are immutable, they cannot be changed. However, List
s are mutable, they can be changed. String methods return a brand new string entirely, because you can't change them. List methods do not return a new list, they modify the existing list, because lists are mutable.
So now that we've seen how to create a
list, let's take a look at a powerful and
0:00
common feature of lists, adding items.
0:04
So, let's go ahead and pop open a REPL.
0:06
So I'm gonna open up
the Python Shell with Python.
0:08
And one thing I should note is that lists
can store any object, any data type.
0:12
So, let's say I wanted to store
my temperature over time.
0:17
As I went to the doctor,
they can just add to my records, right?
0:21
So, let's create an empty
list to store these readings.
0:26
So we'll say temperatures =,
and I'm gonna do an empty list,
0:29
and again that's [ ], empty list, awesome.
0:35
So now, let's add my temperature to that.
0:41
So what we wanna do is,
we want to append a value to our list.
0:44
So it'll say temperatures and there's
a method that allows you to do this.
0:47
So I'm going to put a dot,
because I wanna get to the methods.
0:52
And the method name that allows
you to add a value to the end of
0:57
the list is called append.
1:00
So I'm gonna say append.
1:01
And I will pass in the item and
the first time I was running (98.6).
1:03
Now, don't worry I'm not super sick,
those are in Fahrenheit and
1:09
that's the standard temperature.
1:13
And then the next trip to my
doctor they took my temperature,
1:15
I was running a little bit hot.
1:18
So I'm gonna append another value.
1:20
We'll do (99.4), and
let's take a look here at our list.
1:21
Here we go, we got two lists in there,
just keep on appending and
1:29
wait until the end of the list, right?
1:31
You'll note that when I'm appending these
values, I'm not being returned a new list.
1:34
I'm actually modifying
the existing list in place.
1:38
That's because lists are mutable,
you're able to change them.
1:42
So I end up catching a flu.
1:46
And it was so
bad that I ended up the emergency room.
1:48
And they stored their
temperatures there for me.
1:51
And this is what it look like in there,
it was pretty bad.
1:55
So these are my er_temps.
1:56
And I'm going to create a new list.
1:58
I was at [102.2, that's really high
if you don't speak in Fahrenheit.
2:00
And then I was 101.1 and
started going back down to 99.9].
2:06
So, when I came back to my doctor,
I wanted to combine the two lists, right?
2:09
It's the same data that
we're talking about,
2:15
it's just from two different sources.
2:16
So, if you wanna add all items from
one list onto an existing list,
2:18
you can use a method known as Extend.
2:23
So I'm gonna use my original list,
which was temperatures.
2:27
I'm going to extend that with my
other value, which is (er_temps).
2:30
Now, if I take a look at temperatures,
2:39
you'll see that I have both of
those lists together, right?
2:41
It just appending these all at the end,
it extended the list.
2:44
Now sometimes, extending a list
isn't exactly what you want.
2:49
You might wanna have a combination
of the two lists, but
2:53
not modify either one of them.
2:56
You want a brand new list and
leave the previous lists alone.
2:57
This is where concatenation
comes into play.
3:02
Concatenation works with the plus sign and
it's just like string concatenation.
3:04
So here, let's do this.
3:09
We'll say that I have some
primary_care_doctors.
3:11
You might of heard of these doctors.
3:13
Go to a doctor called ["Dr Scholls"], and
my other go to doctor is "Dr Pepper"].
3:16
And when I was at the I didn't catch their
names, all I caught was their first names.
3:23
And so, it was ["Doug", and
"Susan"], I was super out of it,
3:29
they were really super attractive though.
3:32
But I don't wanna change my
primary care doctors, right?
3:37
Suppose I wanted to show a list of
everybody who helped me through this
3:41
flu season.
3:45
What I can do is, I can concatenate
those two lists together.
3:45
So if I wanted to say all_doctors.
3:49
And I could say, I wanted to
use my primary_care_doctors and
3:52
I wanted to use my er_doctors.
3:57
That created a brand new list and
stuck it in all_doctors.
4:00
So all_doctors is now referring to a list
that has both 'Doug' and 'Susan' and
4:03
'Dr Scholls' and 'Dr Pepper' together, but
it didn't affect my primary_care_doctors.
4:07
One more thing that I'd like to make sure
we remember is that these lists can store
4:13
any data type, right?
4:18
So, if I wanted to store in my
temperatures, which before we were just
4:20
putting floats in there, right,
cuz they had that numbers.
4:23
If I can append, I can totally append
to the (99), it's an integer, but
4:26
it would go in there.
4:29
And I can even actually, nothing's
stopping me from putting in a string,
4:30
all right, of like ("Burning up").
4:33
And if I take a look at temperatures,
it's totally gonna be fine.
4:35
There's an integer and a string in this
list, the data type doesn't matter.
4:40
Okay, so, back to our meeting example.
4:44
What we were trying to do here is, we
were trying to get Ashley to come along.
4:48
She had pinged me and
I wanted to add her to these list.
4:52
Now, I could just put her here.
4:55
Let's practice our skills,
let's give them a little stretch.
4:56
So let's add her to adhere to this list.
4:59
So what do we do to add her?
5:01
We want her at the end of the list, so
we are just going to append("Ashley").
5:03
There we go.
5:09
And as meetings work, Ashley suggested
that we should also have Guil and
5:10
James come as well.
5:14
So, let's extend our list
with Ashley's suggestions.
5:15
So she suggested that we add ["James",
"Guil"], so that's a list in it's own.
5:19
And we want to just put them
at the end there of our list.
5:25
And since this is a list literal,
we've created a new list, and
5:31
we want to say attendees.extend(["James",
"Guil"]).
5:35
Now, if we accidentally said append,
5:40
it will would actually
append a list to our item.
5:42
We'll get to that later, but
now the list should be extended and
5:45
James and
Guil should be appended to the end.
5:49
Well, you know what, we should probably
add some optional invitees, too.
5:51
These meetings tend to grow, don't they?
5:56
So we'll do optional_invitees.
5:58
And these two are super busy, so
I'm gonna not suggest that they come,
6:01
but they can come if they want to.
6:05
So we got Ben Jacobin and David Farland,
so they're optional invitees here.
6:08
So, you know what now though,
our account's gonna be off here, right?
6:13
Cuz this is only looking at the attendees
but we might need a bigger room.
6:17
So, let's make sure that we have
a large enough meeting space.
6:22
Let's concatenate these two lists.
6:25
So we'll say potential_attendees.
6:28
And we don't want to modify either
the optional or the original attendees,
6:31
we just wanna make a new list.
6:36
So we'll say attendees +
optional_attendees, and this should be
6:38
everybody that could potentially be there
and that we will have a large enough room.
6:45
So let's make sure that this says there
6:50
are a length of potential_attendees,
6:54
say there are potential here.
6:59
We don't need this space
because it's built-in for us.
7:03
There we go.
7:06
And let's give that a run.
7:07
I'm gonna drop out of here, and
I will type python meeting.py.
7:10
And I spelled something wrong,
it looks like, right?
7:16
So NameError: optional_attendees.
7:19
I said invitees.
7:21
So this is optional_invitees.
7:22
There are 8 potential attendees currently,
awesome.
7:30
Now, the order of this list that we're
working with here doesn't really matter.
7:34
But as you can imagine, there are some
list where the order matters.
7:38
And you might not want just
to add the end of your list,
7:42
you might want to insert
at a specific location.
7:45
Let's take a look at that problem
right after this quick break.
7:47
You need to sign up for Treehouse in order to download course files.
Sign up