1
00:00:04,260 --> 00:00:09,690
Let's talk about creating lists now, in
Python. So it may seem strange to have

2
00:00:09,690 --> 00:00:13,590
the video on creating lists, so far into
the section, but some of this wouldn't

3
00:00:13,590 --> 00:00:17,100
have made sense earlier. Now we've
already seen a couple of ways of

4
00:00:17,100 --> 00:00:21,600
creating a list. Firstly, let's open up
our number_lists.py

5
00:00:21,600 --> 00:00:25,940
program that we wrote, and let's review
those methods. So I'm going to delete all but

6
00:00:25,949 --> 00:00:29,910
the first two lines and then add an
empty list at the top, so let's go ahead

7
00:00:29,910 --> 00:00:34,580
and do that. Okay, then back on line 1
what we're going to do, is type empty

8
00:00:34,580 --> 00:00:39,980
underscore list equals left and right
square brackets - so it's an empty list, in

9
00:00:39,989 --> 00:00:44,670
other words, that we've created. Alright,
so we can create a list by using a

10
00:00:44,670 --> 00:00:48,989
list literal. Line one creates an empty
list - there's nothing between the square

11
00:00:48,989 --> 00:00:54,660
brackets. We've also got two other lists,
as you can see on lines 2 & 3. Each list

12
00:00:54,660 --> 00:01:00,179
is a comma separated list of values
enclosed in square brackets. You can also

13
00:01:00,180 --> 00:01:05,920
create a list by concatenating existing
lists. Let's have a go at doing that.

14
00:01:05,920 --> 00:01:13,180
So on line 5 I'm going to type numbers is
equal to even plus odd. And let's just print

15
00:01:13,180 --> 00:01:17,540
out the result, so print and then
numbers in parentheses on the next line.

16
00:01:17,540 --> 00:01:21,610
So the new list here - numbers - contains
all the items from the two lists that

17
00:01:21,610 --> 00:01:28,150
we've concatenated. So let's just run
this to confirm. So notice after I ran it

18
00:01:28,150 --> 00:01:32,780
there, that the two items are in the same
order as they were in the original list.

19
00:01:32,780 --> 00:01:38,260
The odd numbers appear after the even
ones because we added odd to even.

20
00:01:38,260 --> 00:01:42,850
A third way to create a list is to use a
function that returns one. We've seen a

21
00:01:42,850 --> 00:01:47,400
function that does that - the sorted
function. Let's put some code in for that.

22
00:01:47,400 --> 00:01:52,990
So line eight I'm going to type sorted
underscore numbers is equal to sorted,

23
00:01:52,990 --> 00:01:57,759
and in parentheses, numbers. Let's
print out the result of sorted underscore

24
00:01:57,760 --> 00:02:03,580
numbers, or the value of sorted
underscore numbers. Let's run this.

25
00:02:03,580 --> 00:02:07,500
We've now got another list there - sorted underscore numbers - that contains all the

26
00:02:07,509 --> 00:02:12,190
items for numbers but now in order. And
we've seen that before. I mention it

27
00:02:12,190 --> 00:02:16,100
again because it may not have been
obvious that a new list is created.

28
00:02:16,100 --> 00:02:20,760
The original numbers list is still in the
same order as it was, and we can check that

29
00:02:20,769 --> 00:02:33,700
out by printing it out: print numbers, run
it again. If you want to sort a list in

30
00:02:33,700 --> 00:02:38,350
place without creating a new list, then
you'd call the .sort method. If you

31
00:02:38,350 --> 00:02:42,310
want a sorted list without changing the
original, as we've done here, you can use

32
00:02:42,310 --> 00:02:47,530
the sorted function. sorted() will take
any iterable object. We can give it a

33
00:02:47,530 --> 00:02:53,010
string, and a new list will be created
from the characters in the string.

34
00:02:54,360 --> 00:03:01,000
So let's try that: line twelve, digits
equals sorted parentheses double quotes,

35
00:03:01,000 --> 00:03:08,210
432985617.
Then close off the string and

36
00:03:08,210 --> 00:03:12,620
parentheses, and print digits on the next
line with print, parentheses and digits

37
00:03:12,620 --> 00:03:16,850
in the middle of that. Now before I run
the program, have a think about what it's

38
00:03:16,850 --> 00:03:21,290
going to produce. What will the digits
list contain? Pause the video and start

39
00:03:21,290 --> 00:03:24,240
it again when you've worked out your answer.

40
00:03:25,380 --> 00:03:29,560
Okay, so run the program now to see if
you're right and I'll do that. We get a

41
00:03:29,560 --> 00:03:35,409
list of strings. So each string is a
single character - a digit. When you create

42
00:03:35,409 --> 00:03:39,159
a list from another object, the contents
of the list will normally be the same

43
00:03:39,160 --> 00:03:45,960
type as the items in that object. On line
eight we create a list from a list of ints.

44
00:03:45,960 --> 00:03:50,319
The list that was produced - sorted_numbers - is a list of ints.

45
00:03:50,319 --> 00:03:55,060
On line 12, we create a sequence of
characters, and the digits list that's

46
00:03:55,060 --> 00:03:58,150
produced is a list of strings. So in
Python a character is just a string of

47
00:03:58,150 --> 00:04:02,460
length 1. Let's just run that again to
confirm. So we've got integers first,

48
00:04:02,460 --> 00:04:08,040
and we've got a list of strings, or
characters, on the last line of output.

49
00:04:08,040 --> 00:04:11,909
What if you didn't want to sort the
items? You might have noticed the list

50
00:04:11,909 --> 00:04:16,040
function when we looked at the built-in
functions earlier, in a previous video.

51
00:04:16,040 --> 00:04:21,120
Now technically, list isn't a function -
it's a class initializer. But don't worry

52
00:04:21,120 --> 00:04:24,750
about that - you can just call it, just as
if it is a function. And you will be

53
00:04:24,750 --> 00:04:28,020
learning about classes in the
Object-Oriented Programming section,

54
00:04:28,020 --> 00:04:31,410
and then you'll see exactly what list is
doing. Until then we're going to pretend

55
00:04:31,410 --> 00:04:35,070
it's a function, and we can use that
instead of sorted on line 12. So if I go

56
00:04:35,070 --> 00:04:44,160
back and change sorted here, to list,
and I run, this time we still get a list,

57
00:04:44,160 --> 00:04:48,900
but the items are in the same order as
they appeared in the string. You can use

58
00:04:48,900 --> 00:04:53,009
the list function to create a list from
any iterable, and we can use another list,

59
00:04:53,009 --> 00:04:58,650
for example. So let's have a go at
doing that. So line 15 I'm going to type

60
00:04:58,650 --> 00:05:05,460
more_numbers is equal to
list and in parentheses, numbers.

61
00:05:05,460 --> 00:05:10,160
Let's then print that out, so print on the next
line, parentheses more_numbers.

62
00:05:10,160 --> 00:05:15,080
Run the program, and you can see
we get the same list of numbers

63
00:05:15,080 --> 00:05:19,760
that line 10 produced, now in the output on
the last line of output. So why might you

64
00:05:19,770 --> 00:05:24,300
want to do that. Well it's one way to
copy a list. We've now got two lists with

65
00:05:24,300 --> 00:05:29,310
the same items; numbers and more_numbers.
They're not the same

66
00:05:29,310 --> 00:05:32,909
list, though. We can check that in a
couple of ways. I've already shown you

67
00:05:32,909 --> 00:05:38,010
how to print out the IDs, so you could do
that. We can also use is to see if

68
00:05:38,010 --> 00:05:40,860
they're the same, so let's have a go at
doing that.

69
00:05:40,860 --> 00:05:48,020
So line 17 I'm going to type print, and
in parentheses, numbers is more

70
00:05:48,029 --> 00:05:54,299
underscore numbers, then the closing
parenthesis. We'll run this, and you can see

71
00:05:54,300 --> 00:05:58,700
that the output is False, meaning they're
not the same list. They are equal though,

72
00:05:58,700 --> 00:06:03,960
because they contain the same items in
the same order. Let's verify that.

73
00:06:03,960 --> 00:06:11,480
print, this is on line 18, parenthesis numbers is
equal to, so two equal signs, more

74
00:06:11,489 --> 00:06:17,129
underscore numbers, closing parenthesis.
Run the program again now. This time you

75
00:06:17,129 --> 00:06:21,569
can see line 18 has produced the value
True, meaning the lists are equal because

76
00:06:21,569 --> 00:06:25,979
they both contain the same items. However,
the previous line of output from line 17

77
00:06:25,979 --> 00:06:30,269
showed us that they're not the same list.
There's another way to copy lists and

78
00:06:30,269 --> 00:06:33,980
that's to use a slice, and that's
something else we've seen before.

79
00:06:33,980 --> 00:06:38,020
So what I'm going to do is comment out line 15.

80
00:06:39,169 --> 00:06:44,759
We're going to add a new line 16, which
will copy the list using a slice. So on

81
00:06:44,759 --> 00:06:51,169
line 16 I'm going to type more_numbers = numbers,

82
00:06:51,169 --> 00:06:56,980
in square brackets, and then just with
a colon in those square brackets.

83
00:06:56,980 --> 00:07:04,139
And that should be numbers, not number.
So if we run this now, this does the same thing.

84
00:07:04,139 --> 00:07:07,889
We've now got two lists again with the
same items. So another way to create

85
00:07:07,889 --> 00:07:11,999
a list, as you saw there, was to slice an
existing list. There's actually at least

86
00:07:11,999 --> 00:07:16,919
12 different ways to copy a list in
Python. I don't suggest you use most of

87
00:07:16,920 --> 00:07:20,820
them, because they're not very readable,
and some are also not very efficient.

88
00:07:20,820 --> 00:07:25,100
The most readable way is to use the copy
method that was added to lists in

89
00:07:25,109 --> 00:07:32,429
pythons 3.3. So let's comment out line 16
I'm going to replace that, this time on

90
00:07:32,429 --> 00:07:42,809
line 17, with more_numbers is
equal to numbers.copy, this time with

91
00:07:42,809 --> 00:07:45,999
parentheses because we do want to call
the method.

92
00:07:45,999 --> 00:07:51,999
If you run this you can see we still got
the same output. A lot of code you'll

93
00:07:51,999 --> 00:07:56,079
come across will have been converted
from Python 2. That means you'll see the

94
00:07:56,079 --> 00:08:00,999
slice I've now commented out, on line 16,
being commonly used to copy a

95
00:08:00,999 --> 00:08:06,009
sequence. In Python 3, the slice still
performs quite well. The fastest way,

96
00:08:06,009 --> 00:08:10,700
though, involves using star to unpack the
list, but we haven't covered that yet.

97
00:08:10,700 --> 00:08:13,899
We'll look at that in the section on
functions. If you want to check out the

98
00:08:13,899 --> 00:08:17,829
various ways of copying lists and their
relative performance, there's an

99
00:08:17,829 --> 00:08:22,020
interesting Stack Overflow post here. I'm
just going to bring up a browser.

100
00:08:22,020 --> 00:08:27,819
The list is in the resources section as
always. So this is interesting for a

101
00:08:27,820 --> 00:08:32,120
number of reasons, not the least of which
is that it benchmarks the various approaches.

102
00:08:32,120 --> 00:08:35,419
So check that out at your leisure. There's some good information in there

103
00:08:35,419 --> 00:08:42,280
if you want to find out more. Alright, so back to the code.

104
00:08:42,280 --> 00:08:47,040
So this video didn't contain much that was new but
hopefully, you found the revision helpful.

105
00:08:47,040 --> 00:08:50,300
We've seen a few different ways to
create lists and a few ways to copy an

106
00:08:50,320 --> 00:08:54,840
existing list. It may not have been
obvious that you can test lists for equality.

107
00:08:54,840 --> 00:08:58,899
== will return True if the lists contain the same items, in

108
00:08:58,899 --> 00:09:03,430
the same order. The is operator will
return True if two variables are

109
00:09:03,430 --> 00:09:07,629
referring to the same list. So moving on
now, in the next video we'll go back to

110
00:09:07,629 --> 00:09:11,230
that table on operations on mutable
sequence types, and we'll see how to

111
00:09:11,230 --> 00:09:16,620
replace slices in a list. See you in the
next video.

