1
00:00:00,570 --> 00:00:03,340
Hello and welcome to this video.

2
00:00:03,390 --> 00:00:14,490
In this video I will be showing you how to access the elements of a list so you can access the elements

3
00:00:14,640 --> 00:00:18,650
of values in a list by using the index.

4
00:00:18,750 --> 00:00:26,650
The index basically refers to the position of the elements in a list with a pint on list.

5
00:00:26,760 --> 00:00:29,020
The index are zero based.

6
00:00:29,190 --> 00:00:30,290
What that means.

7
00:00:30,570 --> 00:00:36,840
The first element or item in the list is given a position of zero.

8
00:00:36,840 --> 00:00:38,730
So let's take a look at an example.

9
00:00:39,210 --> 00:00:43,530
So let's look at this list here which is called animals.

10
00:00:43,530 --> 00:00:48,770
So with this list if we want to access say the bear.

11
00:00:49,080 --> 00:00:51,810
This will be Position ZERO.

12
00:00:51,990 --> 00:00:56,600
This would be position 1 and this will be position two.

13
00:00:56,880 --> 00:01:00,490
So it is a zero based index.

14
00:01:00,540 --> 00:01:03,600
So let's take a look at how we would do this.

15
00:01:03,630 --> 00:01:11,460
So I've got some code here from a previous video so I'm going to comment out some lines here.

16
00:01:11,520 --> 00:01:16,650
So because I don't want them to be printed I want them to be ignored.

17
00:01:16,650 --> 00:01:20,510
So I just commented out all right.

18
00:01:20,550 --> 00:01:23,270
So these are all commented out now.

19
00:01:23,380 --> 00:01:31,470
So if I wanted to access the bay from this list this is how I would do it.

20
00:01:31,540 --> 00:01:38,200
Type in print and in parentheses I refer to the name of the list.

21
00:01:38,740 --> 00:01:47,610
So the name is called animals and then inside that are the square bracket and type in a zero.

22
00:01:48,100 --> 00:01:50,750
So the zero is the index.

23
00:01:50,750 --> 00:01:53,420
So what that means when this print.

24
00:01:53,650 --> 00:02:01,170
It will just grab the zero index which is the first item or element in the list which is this code bear.

25
00:02:01,690 --> 00:02:12,460
So I'm going to save that and if I open up my command prompt and just typing the name of my file which

26
00:02:12,460 --> 00:02:23,950
is my list dot P Y and press enter and you can see is giving me the value of Bear which is the first

27
00:02:24,010 --> 00:02:25,820
item on the list.

28
00:02:25,870 --> 00:02:30,910
Now if I wanted to access the tiger the tiger I will have an index of 1.

29
00:02:30,910 --> 00:02:35,310
I'll have to replace the 0 with a 1 and so on.

30
00:02:35,310 --> 00:02:43,600
Depending on how many items you have on the list if you notice because I have commented up blocks of

31
00:02:43,600 --> 00:02:50,110
my code the bits I have added a comment to it has not been displayed on the screen.

32
00:02:50,410 --> 00:02:57,700
So comments are quite useful if you want to try different things and you don't want all the code in

33
00:02:57,700 --> 00:03:00,850
your file to be acted upon.

34
00:03:01,240 --> 00:03:09,590
So it blocks out the aspects you don't want to be executed or printed on the screen.

35
00:03:09,700 --> 00:03:15,430
You can also access elements within a list from the back of the list.

36
00:03:15,490 --> 00:03:21,640
So if you're doing a zero based index it would access the elements from the first.

37
00:03:21,640 --> 00:03:35,410
Now if I change this from zero to say negative or one minus one what that will do it will access from

38
00:03:35,410 --> 00:03:39,600
the back of the list it will not give me elephant rather than bed.

39
00:03:40,120 --> 00:03:41,290
Let's check this out.

40
00:03:41,290 --> 00:03:49,060
I'm just going to save the file and open up my command prompt and execute.

41
00:03:49,060 --> 00:03:56,890
You can see now is giving me elephant instead of the bed because I have used a negative when you use

42
00:03:56,890 --> 00:03:58,500
a negative index.

43
00:03:58,690 --> 00:04:03,760
It gives you a value from the back of the list.

44
00:04:03,760 --> 00:04:11,820
Another thing you can do with access in list items is that you can access a range.

45
00:04:11,860 --> 00:04:21,040
For example I can if I wanted to access save from this to this you can specify that or if you want to

46
00:04:21,040 --> 00:04:26,620
access if you want to start listing from a set an index you can.

47
00:04:26,620 --> 00:04:28,440
So this is Position ZERO.

48
00:04:28,450 --> 00:04:35,590
This is position well let's say I want to just print out anything from position 1 and not 0.

49
00:04:35,590 --> 00:04:44,410
So the way we do that inside my print statement I would just do one and I'll do a call on what that

50
00:04:44,410 --> 00:04:51,280
would do it will print out everything apart from the position of zero.

51
00:04:51,280 --> 00:04:53,220
So let's check that out.

52
00:04:53,570 --> 00:05:03,130
And I'm just going to type in the name of my file which is my list the p y and press enter and you can

53
00:05:03,130 --> 00:05:09,660
see it prints out tiger lion pendant elephant but he skips the bear.

54
00:05:09,670 --> 00:05:22,300
So by using the index number on the colon to print every 10 after that index as you can see here another

55
00:05:22,300 --> 00:05:26,530
thing you can do is you can specify a range.

56
00:05:26,620 --> 00:05:33,450
So let's say I only want the tiger and the lion printed on the screen.

57
00:05:34,000 --> 00:05:40,090
So instead of just doing the one on the column I can come here and add three.

58
00:05:40,150 --> 00:05:44,850
So this is Position ZERO position one position to position three.

59
00:05:44,890 --> 00:05:52,360
What this will do it will print just position one position two but it will not print position number

60
00:05:52,570 --> 00:05:53,590
three.

61
00:05:53,590 --> 00:05:56,280
So let's save that and check that out.

62
00:05:56,370 --> 00:06:00,340
Inside Hart terminal all command prompt.

63
00:06:00,340 --> 00:06:03,920
So I just type in the name of my file which is my list.

64
00:06:03,930 --> 00:06:05,910
The P Y press printer.

65
00:06:06,580 --> 00:06:14,470
And as you can see this is just printed out the tiger and the lion the tiger is position one and the

66
00:06:14,470 --> 00:06:15,990
lion is position 2.

67
00:06:16,000 --> 00:06:20,630
It doesn't print position 3 so I'm giving it a range.

68
00:06:20,650 --> 00:06:25,180
So I'll say print grab the values from position 1 and 2.

69
00:06:25,270 --> 00:06:34,750
All right does what that does on the final thing I want to show you in this video is how to modify the

70
00:06:34,750 --> 00:06:35,250
lists.

71
00:06:35,260 --> 00:06:41,320
So if you want to change the item in the list let's say this item called Bear.

72
00:06:41,350 --> 00:06:46,500
I want to change that to a dog the way I would do that.

73
00:06:46,630 --> 00:06:55,600
I just type in the name of the list which is animals and how add the square bracket and add the index

74
00:06:55,690 --> 00:06:58,850
of what I want to change which is the bear.

75
00:06:58,860 --> 00:07:08,860
He has a index of zero and then I use the equals to sign to set a new value for what I want to change

76
00:07:08,860 --> 00:07:09,370
it to.

77
00:07:09,820 --> 00:07:14,970
So let's say I want to change the value of the bear to the dog.

78
00:07:14,980 --> 00:07:16,800
That's how I would do that.

79
00:07:16,810 --> 00:07:20,680
So I do control is to save on a Mac.

80
00:07:20,680 --> 00:07:22,280
You can do command s.

81
00:07:22,570 --> 00:07:35,830
Now I can test this if I do a print inside that print and type in the name of my list I'm passing the

82
00:07:35,830 --> 00:07:37,460
index of 0.

83
00:07:37,480 --> 00:07:45,640
Now this index of 0 will no longer see bear it should now see dog because I have made a modification

84
00:07:45,640 --> 00:07:46,690
to that name.

85
00:07:46,930 --> 00:07:49,110
So let's save that.

86
00:07:49,240 --> 00:07:52,000
Let's go back into my command prompt.

87
00:07:52,000 --> 00:08:02,730
I type in my list the p y and press enter and as you can see is giving me the value of dog.

88
00:08:02,830 --> 00:08:04,960
So when you executing code it would do it.

89
00:08:04,960 --> 00:08:06,370
Line by line.

90
00:08:06,880 --> 00:08:10,880
So this here is printing out this code here on line 5.

91
00:08:10,900 --> 00:08:19,600
That's this line and then this print here on May 7 refers to this dog that I am telling it to grab the

92
00:08:19,600 --> 00:08:21,350
value of that index 0.

93
00:08:22,000 --> 00:08:30,340
So that's basically how you can play around and access values and also modify values within a list.

94
00:08:30,340 --> 00:08:32,400
Thanks for watching and bye for now.
