1 00:00:00,420 --> 00:00:01,410 Instructor: Welcome back. 2 00:00:01,410 --> 00:00:05,460 I kept mentioning this key term, iterable, 3 00:00:05,460 --> 00:00:08,220 but what does it really mean? 4 00:00:08,220 --> 00:00:10,350 Iterable, you're gonna hear that over 5 00:00:10,350 --> 00:00:12,000 and over in Python programming 6 00:00:12,000 --> 00:00:14,820 and other programming languages as well. 7 00:00:14,820 --> 00:00:18,210 And as we discussed a little bit in the previous video, 8 00:00:18,210 --> 00:00:22,500 iterable simply means it is an object 9 00:00:22,500 --> 00:00:26,730 or a collection that can be iterated over. 10 00:00:26,730 --> 00:00:31,730 So again, an iterable can be a list, 11 00:00:31,860 --> 00:00:33,750 can be a dictionary, 12 00:00:33,750 --> 00:00:36,690 can be a tuple, can be a set. 13 00:00:36,690 --> 00:00:38,160 It's a collection of items. 14 00:00:38,160 --> 00:00:42,870 We also saw that a string could be iterable. 15 00:00:42,870 --> 00:00:45,750 Now these things are iterable. 16 00:00:45,750 --> 00:00:46,583 Why? 17 00:00:46,583 --> 00:00:49,503 Because they can be iterated. 18 00:00:50,850 --> 00:00:54,090 That is iterated means that we can go one 19 00:00:54,090 --> 00:00:57,810 by one to check 20 00:00:57,810 --> 00:01:00,303 each item in the collection. 21 00:01:03,330 --> 00:01:04,260 And you're gonna hear this 22 00:01:04,260 --> 00:01:07,380 over and over when talking to Python programmers. 23 00:01:07,380 --> 00:01:11,100 So you wanna make sure that you have that vocabulary. 24 00:01:11,100 --> 00:01:13,290 You're iterating over something, 25 00:01:13,290 --> 00:01:16,170 you're looping over something. 26 00:01:16,170 --> 00:01:19,110 So iterable is the noun 27 00:01:19,110 --> 00:01:21,930 and iterated or iterate 28 00:01:21,930 --> 00:01:23,910 is the action 29 00:01:23,910 --> 00:01:26,850 of iterating over an iterable. 30 00:01:26,850 --> 00:01:29,250 I know, it's a little confusing. 31 00:01:29,250 --> 00:01:32,730 Now, many objects in Python, like I said, are iterable, 32 00:01:32,730 --> 00:01:37,020 but there's a few special ones that we haven't covered yet, 33 00:01:37,020 --> 00:01:38,580 specifically, dictionary. 34 00:01:38,580 --> 00:01:43,110 We saw how lists, tuple, sets, and strings can be iterated. 35 00:01:43,110 --> 00:01:45,720 But what about an object? 36 00:01:45,720 --> 00:01:48,540 Let's say we have an object here, 37 00:01:48,540 --> 00:01:51,070 and this object will be users 38 00:01:51,930 --> 00:01:55,000 and users or user 39 00:01:57,422 --> 00:01:59,133 will have a name of Golem, 40 00:02:00,090 --> 00:02:04,050 will have age of 5,006, 41 00:02:04,050 --> 00:02:05,520 Golem's, pretty old. 42 00:02:05,520 --> 00:02:10,342 And then we'll say, can swim Golem, 43 00:02:11,880 --> 00:02:14,163 let's say, cannot swim, false. 44 00:02:15,150 --> 00:02:18,400 What if we use this object right here 45 00:02:20,850 --> 00:02:24,640 for item in user and we print 46 00:02:26,760 --> 00:02:28,113 right here, the item. 47 00:02:29,250 --> 00:02:30,630 What do you think will happen? 48 00:02:30,630 --> 00:02:31,593 Let's run this. 49 00:02:33,210 --> 00:02:38,100 Well, I get an error because I forgot a semicolon here. 50 00:02:38,100 --> 00:02:41,013 And we don't want Eagles, we want semicolon. 51 00:02:42,150 --> 00:02:45,453 And then obviously, this needs to be a string as well. 52 00:02:46,650 --> 00:02:49,740 Now if I click run here, look at that. 53 00:02:49,740 --> 00:02:53,400 I get name, age, and can swim. 54 00:02:53,400 --> 00:02:58,400 So I've printed the keys of the dictionary. 55 00:02:58,860 --> 00:03:01,590 Now this dictionary, we have the keys. 56 00:03:01,590 --> 00:03:02,550 That's great. 57 00:03:02,550 --> 00:03:06,420 But, hmm, what if I wanted actually the values? 58 00:03:06,420 --> 00:03:08,463 Is there a way to do this? 59 00:03:09,570 --> 00:03:13,920 Well, dictionaries have three methods that are really, 60 00:03:13,920 --> 00:03:18,300 really useful when we wanna loop over 61 00:03:18,300 --> 00:03:20,190 their keys and values. 62 00:03:20,190 --> 00:03:22,203 The first one is items. 63 00:03:23,640 --> 00:03:28,083 And with items, when I do this and I click run, 64 00:03:29,670 --> 00:03:33,600 you'll see that I get the key value pair 65 00:03:33,600 --> 00:03:35,163 in a tuple. 66 00:03:36,180 --> 00:03:39,663 And this is a very common pattern that you'll see a lot. 67 00:03:40,560 --> 00:03:45,560 Another one that we have is values. 68 00:03:46,620 --> 00:03:48,423 And guess what that does? 69 00:03:50,550 --> 00:03:52,870 That gives us the values 70 00:03:54,243 --> 00:03:55,563 of the dictionary. 71 00:03:57,090 --> 00:04:01,020 And we saw that user just leaving it blank gives us 72 00:04:01,020 --> 00:04:04,530 the keys, but we have a method that is more descriptive 73 00:04:04,530 --> 00:04:09,060 and allows us to show exactly what we wanna do, 74 00:04:09,060 --> 00:04:10,440 which is keys. 75 00:04:10,440 --> 00:04:13,530 And if we run this, you'll see 76 00:04:13,530 --> 00:04:16,923 that I'm able to iterate over the keys. 77 00:04:18,360 --> 00:04:21,269 And these three methods are very common 78 00:04:21,269 --> 00:04:24,360 and you're gonna use them a lot in your programming career. 79 00:04:24,360 --> 00:04:27,540 So make sure you take note of them. 80 00:04:27,540 --> 00:04:29,430 Items, values, and keys. 81 00:04:29,430 --> 00:04:32,943 It allows us to iterate over dictionaries. 82 00:04:33,810 --> 00:04:36,480 But another interesting thing that you can do 83 00:04:36,480 --> 00:04:39,150 and another common pattern when iterating 84 00:04:39,150 --> 00:04:43,953 over dictionaries is what if you wanna print for example, 85 00:04:45,763 --> 00:04:49,073 separately the item's name and Golem. 86 00:04:49,920 --> 00:04:52,920 So right now, we're returning a tuple 87 00:04:52,920 --> 00:04:56,040 and we saw that we can do tuple unpacking, right? 88 00:04:56,040 --> 00:04:57,780 I can say, 89 00:04:57,780 --> 00:05:02,430 key value equals item 90 00:05:02,430 --> 00:05:05,640 and then just print key value. 91 00:05:05,640 --> 00:05:09,780 If I run this, you see that I'm able 92 00:05:09,780 --> 00:05:11,373 to print these, 93 00:05:12,450 --> 00:05:14,580 but there's actually a shorthand way 94 00:05:14,580 --> 00:05:16,800 of being able to do this. 95 00:05:16,800 --> 00:05:20,970 In here in the for loop, I can say key value 96 00:05:23,400 --> 00:05:26,733 and just completely avoid this entire line. 97 00:05:27,690 --> 00:05:29,043 And if I run this, 98 00:05:30,240 --> 00:05:31,073 there you go. 99 00:05:31,073 --> 00:05:32,550 It still works. 100 00:05:32,550 --> 00:05:35,430 And this again is a very common pattern 101 00:05:35,430 --> 00:05:38,640 where if you're collecting the items of a dictionary, 102 00:05:38,640 --> 00:05:40,890 you can separate them into key and value, 103 00:05:40,890 --> 00:05:44,253 or you might even see shorthand KMV. 104 00:05:46,350 --> 00:05:49,470 Again, these can be any variables that you want. 105 00:05:49,470 --> 00:05:52,410 You can name them whatever helps your code 106 00:05:52,410 --> 00:05:53,373 be more readable. 107 00:05:54,300 --> 00:05:56,400 By the way, just for fun, let's say, 108 00:05:56,400 --> 00:06:01,400 in the last line here, I wanna iterate over the number 50. 109 00:06:02,220 --> 00:06:04,983 What do you think will happen then if I click run? 110 00:06:06,210 --> 00:06:08,970 Sorry, name error, name key. 111 00:06:08,970 --> 00:06:10,470 Oh, I get an error here 112 00:06:10,470 --> 00:06:13,830 because I haven't changed these, so let's change these back. 113 00:06:13,830 --> 00:06:15,690 That's not the error I was expecting. 114 00:06:15,690 --> 00:06:19,650 The error I was expecting was this, 115 00:06:19,650 --> 00:06:22,410 TypeError: "int" object 116 00:06:22,410 --> 00:06:24,093 is not iterable. 117 00:06:24,093 --> 00:06:26,370 Now, this error should make sense now 118 00:06:26,370 --> 00:06:29,370 because I've said it over and over. 119 00:06:29,370 --> 00:06:30,930 Why is it not iterable? 120 00:06:30,930 --> 00:06:35,520 Well, because it's not an object that can be iterated over. 121 00:06:35,520 --> 00:06:37,410 It's not a collection of items 122 00:06:37,410 --> 00:06:39,390 because we learned that in Python, 123 00:06:39,390 --> 00:06:41,460 these are iterable. 124 00:06:41,460 --> 00:06:42,990 I'll see you in the next video. 125 00:06:42,990 --> 00:06:43,823 Bye-bye.