Create Lists
00:00 In this lesson, you’ll learn how you can create a list in Python, and you’ll learn about three different ways that you can do that. First one is a list literal.
00:09 This is what you’ve seen in the previous slide. Essentially, having a comma-separated list of values that you surround with square brackets.
00:17
Another option is to use the built-in list()
function. Similar to the tuple()
function, you need to pass it a sequence type inside.
00:24 So these angle brackets that I’m showing here just mean that you don’t actually put in sequence, but this has to be replaced with some sort of a sequence.
00:32 A Python sequence could be a tuple, could be a string,
00:37
and yet another way of doing it is to use a string method, .split()
, on a string, and you can provide it optionally a separator. That means like a character that you want to split the string on.
00:49 If this sounds a little abstract, don’t worry. Let’s go ahead and head over to IDLE and try out all three of these options.
00:57
I’ll start with the list literal, so I can make a numbers list by writing numbers =
and then opening up square brackets, putting in my values 1, 2, 3
, or anything and then closing the square brackets.
01:14
And now when I press Enter, I have a list consisting of three values, and I can also prove that this is a list by passing the variable to the type()
function.
01:26
And you can see that Python tells me this is of the class list
. So this is how you can create a list literal. Again, it doesn’t have to contain integers, it could contain anything.
01:36
Just like with tuple
, this can be a mix of elements as well. So here’s a string, then here’s an integer, and then here’s a floating-point number, for example.
01:48
And I could even put a tuple in here as well, (1, 2, 3)
, and then close the whole list. So this is a valid list. I’ll go ahead and assign it to a variable so that we can look at it a little more.
02:06 because it contains a lot of strange values that don’t seem to have any relation to each other. All right, so this messy list, I can also use indexing to access elements,
02:17 but this is not what we’re talking about, right? We’re just about creating lists for now. So I’ll try not to drift too far away and come back to actually creating lists, right?
02:26
So the first way to do it, as you’ve seen, is using a list literal: square brackets, comma-separated values in there, values of any type. All right, so the second way that we talked about is using the list()
function.
02:39
So you can also use list()
and then pass in a sequence type, for example, a string,
02:49 and that’ll also create a list.
02:51
You see the square brackets at the beginning and the end, and then it’s strings, single-character strings, separated by commas. Looks very similar to the tuple()
function that you’ve used earlier, and there’s other ways to do it as well.
03:05
So let’s look at the one that I’ve mentioned in the slides, which is using the string method .split()
. For this, you need to start off with a string.
03:14
Let’s say we have "Hello, World"
, the string, and then you can call .split()
on it and optionally pass a separator in here. But let’s see what it does without a separator.
03:29
You can see that Python creates a list. Again, you can see the square brackets at the beginning and the end, and it now contains two strings. One is "Hello,"
and the second one is "World"
.
03:41
So from this, you might see that where it split the string is at the space character. So whitespace is the default argument for .split()
to separate a string into kind of words like it’s an approximation for separating strings into words, but you can also pass a separator in there and then get different results.
04:02
So if I do the same, but instead of not passing an argument in here and we’re going to put, let’s say a comma, that’s going to be quite similar, then you can see now it’s split on the comma, and that comma that used to be part of "Hello"
is gone because that gets eaten up by splitting it.
04:21 And the whitespace in this case is still part of the second string.
04:27
And if you split on "o"
,
04:31
you again get a different result. Now you can see that we get "Hell"
as the first one. Here’s a split. Then the second string is going to be ", W"
.
04:45
And then we have another "o"
, so Python uses this to split the string again, and then the third string in the list is going to be "rld"
, so just the rest of the original string.
04:56
I would say that probably the most common way to use this .split()
method is to split on whitespace. So imagine you have a sentence, "The quick brown fox"
, and then you call .split()
on it without passing an argument.
05:12
Then as a result, you get a list that actually just contains the words 'The'
, 'quick'
, 'brown'
, and 'fox'
.
05:19
So this is probably the most common way of using .split()
to create a list of strings. But you could also specify with, for example, maybe you have comma-separated values and you want to put them into a list, and you could use that .split()
with the comma as the argument.
05:35 But to be honest, there’s probably better ways of dealing with comma-separated values than using this.
05:41 Anyways, long story short, you’ve seen three ways of creating a list. First one was a list literal, which means just putting comma-separated values and surrounding it by square brackets.
05:53
In this case, the square brackets are not optional, unlike the parentheses are with tuple()
. If you think about it, that makes sense because if you would leave off the square brackets here, then you would create a tuple, right?
06:04 Because there you could just put the comma-separated values and it still creates a tuple. So here you need to put the square brackets in order to actually make a list.
06:14
Then secondly, we’ve looked at using the built-in list()
function, and there you can pass in a sequence like a string or a tuple. And finally, we’ve looked at using the string method .split()
to create a list from a string,
Become a Member to join the conversation.