1
00:00:04,660 --> 00:00:09,540
Alright, so let's talk a bit more about nested
indexing. At the end of the last video,

2
00:00:09,540 --> 00:00:14,000
I left you experimenting with indices
in our nested data structure. You should be

3
00:00:14,000 --> 00:00:18,350
able to get the details for any album
and song, from the list. Now if we want

4
00:00:18,350 --> 00:00:22,400
to print the song Mayhem, which I talked
about, we need to start with the fourth

5
00:00:22,400 --> 00:00:27,180
album in the list. That's at index
position three. So go down to our code here.

6
00:00:27,180 --> 00:00:31,920
So we need to change line 47, to access
the fourth album - so we change the

7
00:00:31,920 --> 00:00:38,200
2 to a 3 there. The index for the
songs list stays the same - all the songs

8
00:00:38,200 --> 00:00:42,860
lists for each album, are at index
position three - so we don't need to change that at all.

9
00:00:42,860 --> 00:00:45,920
But Mayhem is the third song, or is the

10
00:00:45,920 --> 00:00:51,200
third item rather, in the songs for that
album, so we use index position 2 for that.

11
00:00:51,200 --> 00:00:57,960
So on line 53, I'll change the 1 to a 2. 
The song title itself, is at the same position -

12
00:00:57,960 --> 00:01:01,420
position 1 in the tuple - 
so the rest of the code stays the same.

13
00:01:01,420 --> 00:01:07,340
Run the program. We confirm we get
Mayhem printed out. Alright, so let's

14
00:01:07,340 --> 00:01:11,780
see how we can do that all in one go.
So we come down, add some code:

15
00:01:11,780 --> 00:01:21,200
line 57, I'm going to type mayhem is
equal to albums, left square bracket and

16
00:01:21,200 --> 00:01:25,460
3 in it, another set of square
brackets with 3 in it, and another

17
00:01:25,460 --> 00:01:29,750
set of square brackets with 2 in it,
and the final set of square brackets

18
00:01:29,750 --> 00:01:36,480
with 1 in it, and let's print mayhem
on the next line.

19
00:01:36,480 --> 00:01:41,120
Now before running the program,  compare those index numbers with the value's that we've used

20
00:01:41,120 --> 00:01:49,240
on lines 47, 50, 53 and 55. You can probably
work out what will be printed.

21
00:01:49,240 --> 00:01:57,420
If not, my choice of variable name's giving it away.
Let's run the program and see.

22
00:01:57,420 --> 00:02:01,420
We get Mayhem printed out again.

23
00:02:01,420 --> 00:02:06,960
So I said I was gonna take this slow, and I've just thrown
that horrible-looking line 57 at you.

24
00:02:06,960 --> 00:02:11,980
We've seen what it does - now we'll look
at how it's done it. I'll take it step by step,

25
00:02:11,980 --> 00:02:18,240
starting with the first index. Let's go 
and add some code: print albums

26
00:02:18,240 --> 00:02:22,680
and in square brackets, the number 3, then closing parenthesis.

27
00:02:22,680 --> 00:02:28,040
Now albums 3 is the tuple for the Imelda May
album. It's got four items, two strings

28
00:02:28,040 --> 00:02:32,690
an int for the year and a list. Let's run
that to confirm, and we can see the

29
00:02:32,690 --> 00:02:38,900
output there, confirming that. So looking
at that, you can see the list of songs

30
00:02:38,900 --> 00:02:43,020
is at position three, and we can add that
index to get just the songs lists.

31
00:02:43,020 --> 00:02:47,140
So let's have a go at doing that.
Alright, so I'll duplicate line 60 again.

32
00:02:47,150 --> 00:02:51,049
I'm gonna go down to the end of the line. I'm
gonna add to the end of the line,

33
00:02:51,049 --> 00:02:56,870
a 3 in square brackets. And again, that's
for the list of songs. Run the program

34
00:02:56,870 --> 00:03:00,700
and we should get the list of songs, and
you can see that is the case down the

35
00:03:00,700 --> 00:03:07,880
bottom there. Alright, back on line 47, we
bound the variable album to albums 3,

36
00:03:07,880 --> 00:03:12,800
and then on line 50, we bound
songs to the item at index position 3

37
00:03:12,800 --> 00:03:18,640
in the album. So looking at the
code, line 61 is producing the same thing

38
00:03:18,640 --> 00:03:23,820
without using intermediate variables. So
now if we want to print the Mayhem tuple,

39
00:03:23,820 --> 00:03:28,180
we can add index 2 to the end.
So let's duplicate the line again,

40
00:03:28,180 --> 00:03:33,220
and come down to the end and add 2 in
square brackets. Run the program again to

41
00:03:33,230 --> 00:03:37,579
confirm what we get. This time, you can
see we get the third item from the songs

42
00:03:37,580 --> 00:03:42,440
list, which you can see above there - that
line of output - the item at position 2.

43
00:03:42,440 --> 00:03:47,380
And that's a tuple, and then we can
index into that to get just the title.

44
00:03:47,389 --> 00:03:51,380
So let's have a go at doing that again,
duplicate the line again. So I'll add 1

45
00:03:51,380 --> 00:03:58,160
in square brackets, on that line. We'll
run it again, and we should see the title

46
00:03:58,160 --> 00:04:01,910
printing out, as you can see, and it is on
the screen there. So each index indexes

47
00:04:01,910 --> 00:04:06,109
into the object produced by the previous
one, as those four lines of code

48
00:04:06,109 --> 00:04:11,419
demonstrate. Alright, so the next video isn't
a video - it's a document giving you

49
00:04:11,420 --> 00:04:14,820
some more things to practice, to help you
get used to indexing into a

50
00:04:14,820 --> 00:04:17,680
nested structure. So have a look at that document,

51
00:04:17,690 --> 00:04:21,220
then I'll see you in the video following that.

