1
00:00:04,680 --> 00:00:08,840
Alright, so let's talk about nested data structures in more detail.

2
00:00:08,840 --> 00:00:12,980
We've had a think about how we're going
to represent the songs in our list of albums.

3
00:00:12,980 --> 00:00:17,820
Now at the moment, we've defined
albums as a list on line 1 of this file -

4
00:00:17,820 --> 00:00:22,640
tuples_intro.py.
Inside the list, we've got five tuples.

5
00:00:22,640 --> 00:00:26,220
I'm going to take this slow,
because nested data structures is

6
00:00:26,220 --> 00:00:30,179
something that new programmers often
find confusing, but they're really quite

7
00:00:30,179 --> 00:00:33,870
straightforward, once you get the hang of
them. But at first, they can seem very

8
00:00:33,870 --> 00:00:38,010
complicated. So let's start by adding
some songs to each of the tuples in the

9
00:00:38,010 --> 00:00:44,000
list. So we're going to create a new Python
file here, called nested_data.

10
00:00:51,200 --> 00:00:56,250
So we recommend that you type in all the
code in this course. Typing the code

11
00:00:56,250 --> 00:01:00,720
really helps you get used to the syntax,
and if you make errors, then fixing them

12
00:01:00,720 --> 00:01:04,559
is great practice. But typing data's
just boring, so what I'm gonna do here is

13
00:01:04,559 --> 00:01:08,700
paste in the data. You can get what I've
pasted it, in in the resources section for

14
00:01:08,700 --> 00:01:15,280
this video. So copy the contents to your
clipboard and paste them in.

15
00:01:15,280 --> 00:01:19,340
Alright, so that's what I've pasted in. You can
see that we've got a list here,

16
00:01:19,350 --> 00:01:25,110
containing four tuples, just as we had
before. Each tuple now contains four

17
00:01:25,110 --> 00:01:29,370
items. So we've got the album title,
artist and year - you see an example of

18
00:01:29,370 --> 00:01:33,180
that on line two - and that's just like
it was before, but now we've also got

19
00:01:33,180 --> 00:01:37,040
a list in each tuple. You can see the
first list, starting on line three,

20
00:01:37,040 --> 00:01:42,080
extending down to line nine. Now, we can
use a for loop to print out the items in

21
00:01:42,090 --> 00:01:45,420
each tuple, and it's going to be very
similar to our previous for loop.

22
00:01:45,420 --> 00:01:49,170
We'll just add an extra variable for the
songs. So let's go down to the bottom of the

23
00:01:49,170 --> 00:01:55,409
source code and add that. So I'm going to
type for name comma artist comma year

24
00:01:55,409 --> 00:02:01,409
comma songs in albums : I'm just going
to print them out, so print

25
00:02:01,409 --> 00:02:07,760
parentheses, and in double quotes Album : left and
right curly brace comma, and we're

26
00:02:07,760 --> 00:02:11,360
going to do the same for the other three.
So we've got Artist :

27
00:02:11,360 --> 00:02:16,120
left and right curly braces comma Year :
left and right curly braces comma

28
00:02:16,120 --> 00:02:21,280
and finally Songs : left and right
curly braces closing double quote.

29
00:02:21,280 --> 00:02:25,560
On the next line I'm going to type
dot format, and in parentheses name comma

30
00:02:25,560 --> 00:02:33,840
artists comma year comma songs.
Let's run the program and then check the

31
00:02:33,840 --> 00:02:37,680
output carefully there. Now the first
three items in each album are simple

32
00:02:37,680 --> 00:02:41,730
variables, the title artist and year. The
fourth item is a list, and we can see

33
00:02:41,730 --> 00:02:45,390
that there on screen, if I scroll over
a little bit, we can see the various

34
00:02:45,390 --> 00:02:50,040
songs there. Now that's at index position
3, and we get the list of songs for any

35
00:02:50,040 --> 00:02:55,920
album by indexing into an album's lists.
Now as I said I'm going to take this

36
00:02:55,920 --> 00:03:00,630
slowly. We can do things a lot more
concisely than I'm about to do, but it's

37
00:03:00,630 --> 00:03:04,860
easier to understand if I break it down
first. So I'm going to start by getting a

38
00:03:04,860 --> 00:03:12,120
reference to the budgie album - that's
this third album starting on line 23. So

39
00:03:12,120 --> 00:03:15,720
in the code I'm going to actually type
we'll separate the outputs so I'm just going to do

40
00:03:15,720 --> 00:03:23,400
a print in parentheses on line 45, on line
47 I'm going to type album equals albums and square

41
00:03:23,400 --> 00:03:27,120
brackets and two within the square
brackets then we'll print album on the

42
00:03:27,120 --> 00:03:35,310
next line. Run this, and we can see the
entry there for Budgie's Night Flight

43
00:03:35,310 --> 00:03:41,310
album. Now we can also index into the
album tuple, to get the songs. The list

44
00:03:41,310 --> 00:03:50,780
of songs is at index position 3. Then
come down here and type songs equals

45
00:03:50,780 --> 00:04:02,270
album 3 in square brackets print
songs in parentheses. Now run this

46
00:04:02,270 --> 00:04:08,850
and you can see we've now got just the
list of songs there, on screen. Now we're

47
00:04:08,850 --> 00:04:13,620
not done yet. We can get any particular
song by indexing into the list of songs.

48
00:04:13,620 --> 00:04:17,820
So let's have a go at doing that. Down
here on line 53 I'm going to type song

49
00:04:17,820 --> 00:04:28,370
equals songs and one in square brackets
print, in parentheses song. Run this

50
00:04:29,180 --> 00:04:34,199
and we can see that song is a tuple.
It's track number two, and the item is

51
00:04:34,199 --> 00:04:39,300
Keeping a Rendezvous. Now if we just want
the title, we can index into the tuple

52
00:04:39,300 --> 00:04:45,990
to get the item at position one. Let's
have a go at doing that. So I'm going to

53
00:04:45,990 --> 00:04:50,849
type print in parentheses song and one
in square bracket, or brackets rather,

54
00:04:50,849 --> 00:04:54,500
and then I'm going to do that and run it.

55
00:04:55,789 --> 00:05:01,469
And you can see we get the song title
Keeping a Rendezvous in the output.

56
00:05:01,469 --> 00:05:05,729
There's nothing very complicated about
this, it's just indexing. If the item we

57
00:05:05,729 --> 00:05:10,680
get is another list or tuple, we can
index into that. If that's also a list or

58
00:05:10,680 --> 00:05:15,090
a tuple then we can index again.
Experiment with different albums and

59
00:05:15,090 --> 00:05:18,960
songs, and use the correct index numbers
to refer to them in the albums list.

60
00:05:18,960 --> 00:05:25,889
Now as an example, looking at this code
again, change the indices so that the

61
00:05:25,889 --> 00:05:30,780
last line of output is Mayhem, from the
Imelda May album. In the next video

62
00:05:30,780 --> 00:05:36,650
we'll see how we can do this more
concisely. See you in the next video

