1
00:00:06,110 --> 00:00:07,140
Welcome back everyone.

2
00:00:07,240 --> 00:00:13,320
In this lecture we're going to talk about python lists lists are ordered sequences that can hold a variety

3
00:00:13,320 --> 00:00:14,550
of object types.

4
00:00:14,640 --> 00:00:18,200
They use square brackets and commas to separate objects in the list.

5
00:00:18,270 --> 00:00:23,820
For example here we see a list of five numbers where we have each integer separated by a comma instead

6
00:00:23,820 --> 00:00:29,080
of square brackets list support indexing and slicing just as we did with strings.

7
00:00:29,100 --> 00:00:33,380
Lists can be nested and also have a variety of useful methods that been called off of them.

8
00:00:33,390 --> 00:00:37,590
Let's get started by helping over to the Adam text editor and playing around flips and exploring these

9
00:00:37,590 --> 00:00:39,420
concepts further.

10
00:00:39,420 --> 00:00:41,650
OK let's start off by creating a list.

11
00:00:41,730 --> 00:00:44,260
I'm going to say my list is equal to.

12
00:00:44,460 --> 00:00:46,550
And then we'll say one two three.

13
00:00:46,590 --> 00:00:51,260
Something you shouldn't do is use list as your variable name.

14
00:00:51,270 --> 00:00:56,790
It's really tempting because it's so straightforward but list is actually a built in keyboard in Python

15
00:00:56,790 --> 00:01:02,050
so don't do this so don't use list you something more unique like my list.

16
00:01:02,240 --> 00:01:02,500
OK.

17
00:01:02,550 --> 00:01:04,880
So I set my list equal to 1 2 3.

18
00:01:05,100 --> 00:01:10,310
Let's go ahead and print out my list save that one Python example at PI.

19
00:01:10,350 --> 00:01:12,220
And now I see 1 2 3.

20
00:01:12,390 --> 00:01:13,780
Just like a code for string.

21
00:01:13,830 --> 00:01:19,580
I can also check the length of a list to see how many items are in my list.

22
00:01:19,680 --> 00:01:24,780
So when I run this I get back three because there's three items in my list and if I add in maybe some

23
00:01:24,780 --> 00:01:25,840
more numbers.

24
00:01:25,950 --> 00:01:31,410
So if I add in a bunch of ones and I run this again then it says hey you have seven items in your list

25
00:01:31,410 --> 00:01:34,380
so one two three four five six seven.

26
00:01:34,480 --> 00:01:38,650
OK so that's way you can check the length of the list and see how you can construct the list.

27
00:01:38,650 --> 00:01:42,240
We should also know is that a list can actually hold mixed data types.

28
00:01:42,250 --> 00:01:48,850
So for example I could start off with an integer then put in a floating point number and then even put

29
00:01:48,860 --> 00:01:50,780
in a string like hello.

30
00:01:51,100 --> 00:01:54,460
And my list is not going to complain at all.

31
00:01:54,580 --> 00:01:57,600
Python has no problem holding multiple data types inside of lists.

32
00:01:57,610 --> 00:02:00,110
Integer float string doesn't matter.

33
00:02:00,110 --> 00:02:01,460
A list can hold it.

34
00:02:01,510 --> 00:02:05,310
Now indexing and slicing works the exact same way like the strings.

35
00:02:05,320 --> 00:02:08,400
For example if I wanted to grab Hello off this list.

36
00:02:08,560 --> 00:02:13,450
Well I just kind of read the index positions 0 1 2.

37
00:02:13,600 --> 00:02:19,270
So in opposition to the third item in this list is hello so I can say my list.

38
00:02:19,270 --> 00:02:21,350
Square brackets to.

39
00:02:21,790 --> 00:02:25,130
And then when I run this I get back hello as the output.

40
00:02:25,520 --> 00:02:33,690
OK so let's try this with another example say A B C D.

41
00:02:34,580 --> 00:02:37,960
So see if you can practice grabbing a certain letter from this list.

42
00:02:38,030 --> 00:02:39,090
Try grepping B.

43
00:02:39,150 --> 00:02:45,600
So it's kind of over 0 1 pass and 1 and then you get BIAZON boy right here B.

44
00:02:45,780 --> 00:02:47,490
And then I can also do slicing.

45
00:02:47,490 --> 00:02:49,600
So let's imagine I wanted to grab.

46
00:02:49,680 --> 00:02:52,360
Let's go ahead and add one more letter here in this list.

47
00:02:52,620 --> 00:02:54,530
Let's say I want to grab a b c d.

48
00:02:54,600 --> 00:03:01,140
That sub list or that slice of the list will I want to start off next to one and then I want to go up

49
00:03:01,140 --> 00:03:03,160
to but not including the last.

50
00:03:03,180 --> 00:03:06,720
So that's 0 1 2 3 4.

51
00:03:06,910 --> 00:03:10,330
So that's slicing the exact same way we used to do it with strings.

52
00:03:10,330 --> 00:03:12,560
Except now we're operating on a list.

53
00:03:12,670 --> 00:03:18,490
So in one Python example out pi I get back BCT because I'm starting it in additional 1 and then going

54
00:03:18,490 --> 00:03:20,790
up to but not including in solution 4.

55
00:03:20,860 --> 00:03:26,230
So that's going to be B C D or 1 2 and 3 now.

56
00:03:26,250 --> 00:03:30,200
Begin to cover some basic list methods so I have my list.

57
00:03:30,230 --> 00:03:31,410
ABC the E.

58
00:03:31,580 --> 00:03:34,240
Let's imagine I wanted to add an item to this list.

59
00:03:34,280 --> 00:03:42,490
What I could do is I could just say my list dot append open close parentheses and they just type in

60
00:03:42,500 --> 00:03:49,700
what I want to append to the very end of the list so I can say Z and I'm going to just print off my

61
00:03:49,700 --> 00:03:57,060
list here and then when I run this I get back ABC and then Z.

62
00:03:57,280 --> 00:03:59,290
So what's happening here.

63
00:03:59,410 --> 00:04:01,670
I'm saying my list is ABC the.

64
00:04:02,050 --> 00:04:05,420
And then I say OK grab that list and using this append method.

65
00:04:05,560 --> 00:04:09,250
Add a Z to the end of that list and then print out my list.

66
00:04:09,250 --> 00:04:11,220
Note that I'm not doing this.

67
00:04:11,410 --> 00:04:17,070
I'm not reassigning my list to be equal to my list thought append because append actually occurs what's

68
00:04:17,080 --> 00:04:19,690
known as in place so it doesn't return anything.

69
00:04:19,730 --> 00:04:24,520
And in fact if you were to run this you would get back none because this is actually it's not returning

70
00:04:24,850 --> 00:04:26,560
anything to be assigned here.

71
00:04:26,560 --> 00:04:32,650
Instead we just say my list append and again that sort of return idea will make a lot more sense when

72
00:04:32,650 --> 00:04:35,240
we create our own functions and own methods.

73
00:04:35,380 --> 00:04:38,230
But right now just keep that in the back of your mind.

74
00:04:38,260 --> 00:04:41,320
OK so we just saw how we can append the end of a list.

75
00:04:41,320 --> 00:04:44,230
What if I wanted to insert maybe somewhere else in the list.

76
00:04:44,230 --> 00:04:48,810
Well I could just use the insert method and then insert.

77
00:04:48,820 --> 00:04:50,430
It's going to take two parameters.

78
00:04:50,530 --> 00:04:55,420
The first one is going to be the index of where you want to insert your item so append only goes the

79
00:04:55,480 --> 00:04:56,500
end of a list.

80
00:04:56,560 --> 00:05:00,630
But if you want to put something at another in the expedition's it says the first or zero in next to

81
00:05:00,630 --> 00:05:01,130
the second.

82
00:05:01,300 --> 00:05:04,930
You can say index 0 comma and then whatever you want to insert.

83
00:05:04,960 --> 00:05:07,540
So let's insert that Z there again.

84
00:05:07,600 --> 00:05:08,830
So here I am saying my list thought.

85
00:05:08,830 --> 00:05:10,870
Insert 0 0.

86
00:05:11,200 --> 00:05:14,030
Save that and now when I say my list here.

87
00:05:14,080 --> 00:05:19,210
Notice now I get back z in the zero position and everything's been shifted over to make that change.

88
00:05:19,330 --> 00:05:23,890
So that's the difference between insert and append append that will just go to the end of the list insert

89
00:05:23,890 --> 00:05:25,720
will go wherever you say so.

90
00:05:25,720 --> 00:05:28,370
So index 0 1 to whatever.

91
00:05:28,720 --> 00:05:31,800
OK now let's imagine I want to remove things from a list.

92
00:05:31,900 --> 00:05:41,320
Well I can use the pop method for that so I can say oops my list got pop and then I can pass in the

93
00:05:41,310 --> 00:05:43,550
in the composition of what I want to remove.

94
00:05:43,610 --> 00:05:48,180
So if I want to move the first element in the list the index which is 0 then that's going to be a.

95
00:05:48,210 --> 00:05:54,150
So it's a pop at this exhibition 0 and then that actually returns an item that's popped.

96
00:05:54,180 --> 00:05:59,090
So it's a popped item is equal to my list the pop.

97
00:05:59,090 --> 00:06:05,880
So then I'll print my list and then also the pop item and I'm actually going to print my list twice.

98
00:06:05,900 --> 00:06:06,430
So we'll see.

99
00:06:06,440 --> 00:06:08,300
Copy and paste that here.

100
00:06:08,360 --> 00:06:10,670
So what I'm going to do is say create my list.

101
00:06:10,850 --> 00:06:12,210
Print out my list.

102
00:06:12,440 --> 00:06:17,360
Pop off the item index zero and save that pop item.

103
00:06:17,480 --> 00:06:22,780
Then print out what mindless looks like and then print out the pop item so let's see what happens.

104
00:06:24,540 --> 00:06:26,380
So I get back e b c d e.

105
00:06:26,390 --> 00:06:28,290
Which makes sense is just printing out that list.

106
00:06:28,290 --> 00:06:29,530
That's from line 2.

107
00:06:29,600 --> 00:06:32,970
Then they pop off the item and it saves the pop item.

108
00:06:33,090 --> 00:06:38,280
And now I've been able to successfully remove in expedition's zero BCT.

109
00:06:38,400 --> 00:06:44,040
And then when I print out my pop item that return back a by default if you don't supply a number here

110
00:06:44,280 --> 00:06:47,840
if you just say that pop pop off the last item in the list.

111
00:06:47,850 --> 00:06:51,110
So when I run this again though this all get back here.

112
00:06:51,290 --> 00:06:53,070
He has the popped off item.

113
00:06:53,310 --> 00:06:56,150
So again no exposition I'll pop off the list.

114
00:06:56,190 --> 00:07:01,300
You can always provide any particular index for this and you want to if I want to pop off an expedition

115
00:07:01,420 --> 00:07:06,810
3 just from that then it goes 0 1 2 3 and it pops off that for me.

116
00:07:06,810 --> 00:07:07,240
OK.

117
00:07:07,530 --> 00:07:10,900
So that's a pen insert and pop.

118
00:07:11,070 --> 00:07:14,920
The last thing I want you to be aware of is you can actually have nested lists.

119
00:07:14,970 --> 00:07:17,350
So let's go ahead and create an example of that.

120
00:07:17,760 --> 00:07:26,400
So I'm going to create a list of say 0 1 2 and I'm going to call this my list 1 and then I'll say my

121
00:07:26,400 --> 00:07:41,780
list to is three four five and then we'll say Maylis three is 670 and then I'll say megaliths is equal

122
00:07:41,780 --> 00:07:49,920
to a list of my list on my list too and my list 3.

123
00:07:50,330 --> 00:07:52,990
Save that and let's print out mega list.

124
00:07:54,730 --> 00:07:59,250
So I saved that and I'm going to run this and those here now I and that's nested list.

125
00:07:59,260 --> 00:08:04,180
So I have three items in mega list and each of those items is a list itself.

126
00:08:04,450 --> 00:08:11,560
So that would mean if I were to check the length of mega list they'll actually just report back three

127
00:08:11,770 --> 00:08:18,160
not nine because as far as this megaliths is concerned it only has three items in it that each of those

128
00:08:18,280 --> 00:08:21,490
items has its own other set of three items.

129
00:08:22,810 --> 00:08:26,720
So just keep that awareness as we talk about Nest lists.

130
00:08:26,740 --> 00:08:29,270
Now let's imagine I want to grab an item from this list.

131
00:08:29,380 --> 00:08:30,650
So I want to grab.

132
00:08:30,910 --> 00:08:32,480
Let's say the number seven.

133
00:08:32,500 --> 00:08:33,550
How would I do that.

134
00:08:33,820 --> 00:08:37,370
Well the number 7 is inside of my list 3 which is inside a mega list.

135
00:08:37,370 --> 00:08:41,540
So the first thing I need to do is actually grab my list three from my list.

136
00:08:41,590 --> 00:08:43,920
So it's going to be 0 1 2.

137
00:08:44,170 --> 00:08:46,030
So my list 3 is an expedition too.

138
00:08:46,030 --> 00:08:46,770
So let's grab that.

139
00:08:46,780 --> 00:08:53,490
Well say square brackets too off of make list and when to run that now I get back six seven eight.

140
00:08:53,500 --> 00:08:57,100
So then what I could do is another set of indexing to grab 7.

141
00:08:57,100 --> 00:09:04,020
So it's going to be at 0 1 so that I pass and one and then I get back just the number seven.

142
00:09:04,020 --> 00:09:09,090
So notice what's happening here mega list of indexing first for that internal list that I'm indexing

143
00:09:09,150 --> 00:09:11,250
again for that actual number.

144
00:09:11,250 --> 00:09:14,820
So this is known as stack indexing with a nested list.

145
00:09:15,100 --> 00:09:15,670
OK.

146
00:09:15,840 --> 00:09:17,490
So that's the basics of this.

147
00:09:17,490 --> 00:09:19,130
Again we talked about nested lists.

148
00:09:19,290 --> 00:09:24,770
The pop a pen insert methods as well as just how to slice an index from a list.

149
00:09:24,820 --> 00:09:26,430
OK we'll see you at the next lecture.
