1 00:00:07,890 --> 00:00:11,230 Welcome back for this way of the programmer segment 2 00:00:11,230 --> 00:00:14,650 on good design practice for creating Nested Data. 3 00:00:14,650 --> 00:00:17,320 Basically, be consistent. 4 00:00:17,320 --> 00:00:20,920 Don't mix two levels of nesting with three for example. 5 00:00:20,920 --> 00:00:26,260 Consider this example, this is bad design practice, on line 1, 6 00:00:26,260 --> 00:00:29,110 we've got a variable nested one which is assigned to a list, 7 00:00:29,110 --> 00:00:32,285 it has a couple elements and that are 8 00:00:32,285 --> 00:00:36,800 numbers and then it's got a few elements that are lists. 9 00:00:36,950 --> 00:00:43,630 Having this mixture is going to make it hard to traverse the Nested Data structure. 10 00:00:44,450 --> 00:00:50,380 So for example, if I try to run this now, I'm going to get an error. 11 00:00:52,130 --> 00:00:55,930 The error tells us that we are trying 12 00:00:55,930 --> 00:01:00,880 to iterate through an integer object on line number 4, 13 00:01:00,880 --> 00:01:02,650 but to understand what's going on, 14 00:01:02,650 --> 00:01:05,965 we need to go up a couple lines first. 15 00:01:05,965 --> 00:01:13,260 So on line number 2, we are iterating through all of the items in nested one. 16 00:01:13,260 --> 00:01:18,130 So X is going to be bound to one and the next time through X is going to be down to two, 17 00:01:18,130 --> 00:01:22,030 and then the third time through X will be bound to the list ABC. 18 00:01:22,030 --> 00:01:25,870 Each time, we're going to print out level 19 00:01:25,870 --> 00:01:32,040 1 just to remind us that we've made it through one more iteration. 20 00:01:32,040 --> 00:01:35,330 But then the action is supposed to happen at line 4. 21 00:01:35,330 --> 00:01:38,765 We've seen something like this before if we really had 22 00:01:38,765 --> 00:01:42,530 lists all the way through is all of the elements of nested one being list, 23 00:01:42,530 --> 00:01:50,165 and we could iterate through each of those items where X was going to be a list like ABC. 24 00:01:50,165 --> 00:01:52,970 We could iterate through it and print out each of 25 00:01:52,970 --> 00:01:58,440 the items saying something like level 2:a. 26 00:02:01,570 --> 00:02:05,480 Our problem is happening when we 27 00:02:05,480 --> 00:02:09,620 get to this for the very first time we've printed level 1, 28 00:02:09,620 --> 00:02:14,225 and then we're getting an error on line 4 because 29 00:02:14,225 --> 00:02:19,245 X is bound not to a list but to the integer, 1. 30 00:02:19,245 --> 00:02:22,730 So when we now say for Y in X, 31 00:02:22,730 --> 00:02:29,575 we're saying for Y in this so that's the Integer object and it is not iterable, 32 00:02:29,575 --> 00:02:31,745 it is not a list. 33 00:02:31,745 --> 00:02:37,390 The problem is really coming because we wanted to get to items like a, and b, 34 00:02:37,390 --> 00:02:40,989 and c which does require this nested iteration, 35 00:02:40,989 --> 00:02:44,140 but our data wasn't structured in a clean way, 36 00:02:44,140 --> 00:02:48,145 so we couldn't just always assume that X was going to be a list. 37 00:02:48,145 --> 00:02:52,340 Sometimes X was going to be a number. 38 00:02:54,590 --> 00:02:58,360 So let's see how to solve that problem. 39 00:02:59,240 --> 00:03:03,385 The basic solution is to use some special case logic. 40 00:03:03,385 --> 00:03:08,790 In our code, we'll check whether the type of X is a list. 41 00:03:08,790 --> 00:03:12,460 If it is, we'll do a nested iteration and if it's not, 42 00:03:12,460 --> 00:03:14,480 we'll do something else. 43 00:03:15,350 --> 00:03:19,360 So that's what's going on, on line 4. 44 00:03:20,720 --> 00:03:26,020 We started iterating X this first time is bound to the value, 45 00:03:26,020 --> 00:03:32,350 1, and we check is the type of X list. 46 00:03:33,260 --> 00:03:37,320 If it is, then we would iterate through it but since 47 00:03:37,320 --> 00:03:40,755 it's not, we're just going to print it. 48 00:03:40,755 --> 00:03:46,699 So I would expect one to show up and then we'll do our second iteration, 49 00:03:46,699 --> 00:03:48,800 X will be bound to two, 50 00:03:48,800 --> 00:03:51,830 and we'll get two to show up in the output window. 51 00:03:51,830 --> 00:03:59,670 The third time, X is going to be bound to a list a, 52 00:03:59,670 --> 00:04:06,575 b and c. Now when we check is the type of X list, 53 00:04:06,575 --> 00:04:08,570 the answer will be true, 54 00:04:08,570 --> 00:04:10,910 and so we'll iterate and for 55 00:04:10,910 --> 00:04:19,065 each Y will be bound to a and then to b and then to c. For each of those, 56 00:04:19,065 --> 00:04:23,310 we'll get something that says level 57 00:04:23,310 --> 00:04:30,445 2:a and level 2:b and so on. 58 00:04:30,445 --> 00:04:37,475 So lets just check my reasoning there and see if that's what actually happens, 59 00:04:37,475 --> 00:04:42,010 and indeed that is what's happening. 60 00:04:42,010 --> 00:04:45,825 Each time we get to line 3, 61 00:04:45,825 --> 00:04:49,420 we print out this level 1, 62 00:04:49,760 --> 00:04:51,945 and the first time, 63 00:04:51,945 --> 00:04:54,390 X was bound to the value 1, 64 00:04:54,390 --> 00:04:56,460 so we just print it. 65 00:04:56,460 --> 00:04:59,070 The second time, we print two, 66 00:04:59,070 --> 00:05:06,980 the third time we iterate through the list abc and we execute line 6, three times. 67 00:05:06,980 --> 00:05:09,680 Once printing out a, once printing out b, 68 00:05:09,680 --> 00:05:13,790 and once printing out c. And you can imagine this would get pretty 69 00:05:13,790 --> 00:05:19,070 complicated with lots of special cases if we had more levels of data nesting, 70 00:05:19,070 --> 00:05:22,970 and we were not consistent about the level of nesting. 71 00:05:22,970 --> 00:05:27,320 The solution when we are in control of the data structure is to make it very 72 00:05:27,320 --> 00:05:31,925 regular with always the same kinds of items in the same level of nesting. 73 00:05:31,925 --> 00:05:34,655 In other words, don't mix integers, lists, 74 00:05:34,655 --> 00:05:38,755 and dictionaries as items in a single list. 75 00:05:38,755 --> 00:05:42,965 Sometimes however, we aren't in control of the data structure. 76 00:05:42,965 --> 00:05:49,445 Maybe our evil predecessor on the project structured the data in a not very careful way. 77 00:05:49,445 --> 00:05:53,995 Or we get it from an external source that structured it however they wanted to. 78 00:05:53,995 --> 00:05:57,950 When that happens, you'll have to notice and add some special case logic, 79 00:05:57,950 --> 00:06:00,980 like the "if statement" that I've got here on line 4. 80 00:06:00,980 --> 00:06:04,205 So that's my advice for following the way of the programmer. 81 00:06:04,205 --> 00:06:06,550 Don't put trip hazards in your way, 82 00:06:06,550 --> 00:06:11,270 make the Nested Data structures you're going to traverse be as regular as possible. 83 00:06:11,270 --> 00:06:14,375 But if somebody else puts the trip hazards in your way, 84 00:06:14,375 --> 00:06:18,200 be prepared to jump over them with some "if-then statements" to handle 85 00:06:18,200 --> 00:06:23,230 differently structured data in different ways. See you next time.