1 00:00:00,420 --> 00:00:01,050 Welcome back. 2 00:00:01,410 --> 00:00:08,910 I kept mentioning this key term iterable, but what does it really mean, iterable? 3 00:00:09,180 --> 00:00:13,940 You're going to hear that over and over in Python programming and other programming languages as well. 4 00:00:14,840 --> 00:00:23,030 And as we discussed a little bit in the previous video, iterable simply means it is an object or a 5 00:00:23,030 --> 00:00:34,010 collection that can be iterated over so again and iterable can be a list, can be a dictionary, can 6 00:00:34,010 --> 00:00:36,530 be a Toppo, can be a set. 7 00:00:36,680 --> 00:00:38,200 It's a collection of items. 8 00:00:38,210 --> 00:00:41,960 We also saw that a string could be iterable. 9 00:00:42,830 --> 00:00:45,740 Now these things are iterable. 10 00:00:45,770 --> 00:00:46,310 Why? 11 00:00:46,460 --> 00:00:49,400 Because if they can be iterated. 12 00:00:50,870 --> 00:00:54,650 That is iterated means that we can go one by one. 13 00:00:56,230 --> 00:01:00,160 To check each item in the collection. 14 00:01:03,290 --> 00:01:08,150 And you're going to hear this over and over when talking to Python programmers, so you want to make 15 00:01:08,150 --> 00:01:15,530 sure that you have that vocabulary, you're iterating over something, you're looping over something. 16 00:01:16,160 --> 00:01:26,420 So iterable is the noun and iterated or iterate is the action of iterating over an iterable. 17 00:01:26,810 --> 00:01:29,540 I know it's a little confusing now. 18 00:01:29,540 --> 00:01:36,560 Many objects in Python, like I said, are edible, but there's a few special ones that we haven't covered 19 00:01:36,560 --> 00:01:38,620 yet, specifically dictionary. 20 00:01:38,630 --> 00:01:42,890 We saw how lists, tuple sets and strengths can be iterated. 21 00:01:43,100 --> 00:01:45,670 But what about an object? 22 00:01:45,680 --> 00:01:58,340 Let's say we have an object here and this object will be users and users or user will have a name of 23 00:01:58,460 --> 00:01:59,000 GOLOMB. 24 00:02:00,080 --> 00:02:12,170 We'll have age of five thousand six gallons, pretty old, and then we'll say can swim, golomb, let's 25 00:02:12,170 --> 00:02:14,000 say, cannot swim false. 26 00:02:15,150 --> 00:02:18,300 What if we use this object right here? 27 00:02:20,840 --> 00:02:24,560 For item in user and we print. 28 00:02:26,740 --> 00:02:27,970 Right here, the item. 29 00:02:29,260 --> 00:02:30,230 What do you think will happen? 30 00:02:30,610 --> 00:02:31,420 Let's run this. 31 00:02:33,180 --> 00:02:40,920 Well, I get an error because I forgot a semicolon here, and we don't Weigel's we want semi-solid. 32 00:02:42,110 --> 00:02:45,310 And then obviously, this needs to be a strength as well. 33 00:02:46,630 --> 00:02:48,070 Now, if I click run here. 34 00:02:49,010 --> 00:02:57,900 Look at that, I get name age and can't swim, so I printed the keys of the dictionary. 35 00:02:58,880 --> 00:03:01,610 Now this dictionary, we have the keys. 36 00:03:01,610 --> 00:03:03,370 That's great, but hmm. 37 00:03:03,710 --> 00:03:06,420 What if I wanted actually the values? 38 00:03:06,440 --> 00:03:08,270 Is there a way to do this? 39 00:03:09,540 --> 00:03:18,660 Well, dictionaries have three methods that are really, really useful when we want to loop over their 40 00:03:18,660 --> 00:03:22,020 keys and values, the first one is items. 41 00:03:23,570 --> 00:03:27,950 And with items, when I do this and I click run. 42 00:03:29,680 --> 00:03:35,020 You'll see that I get the key value pair in a couple. 43 00:03:36,150 --> 00:03:39,540 And this is a very common pattern that you'll see a lot. 44 00:03:40,470 --> 00:03:42,240 Another one that we have. 45 00:03:43,420 --> 00:03:45,460 Is the values. 46 00:03:46,580 --> 00:03:48,260 And guess what that does? 47 00:03:50,530 --> 00:03:53,650 That gives us the values of. 48 00:03:54,630 --> 00:03:55,440 The dictionary. 49 00:03:57,090 --> 00:04:04,530 And we saw that user just leaving it blank gives us the keys, but we have a method that is more descriptive 50 00:04:04,530 --> 00:04:10,380 and allows us to show exactly what we want to do, which is keys. 51 00:04:10,410 --> 00:04:11,790 And if we run this. 52 00:04:13,070 --> 00:04:16,750 You'll see that I'm able to iterate over the keys. 53 00:04:18,330 --> 00:04:24,040 And these three methods are very common, and you're going to use them a lot in your programming career. 54 00:04:24,330 --> 00:04:29,250 So make sure you take note of them items, values and keys. 55 00:04:29,430 --> 00:04:32,790 It allows us to iterate over dictionaries. 56 00:04:33,810 --> 00:04:40,110 But another interesting thing they can do and another common pattern when iterating over dictionaries 57 00:04:40,650 --> 00:04:48,990 is what if you want to print, for example, separately the items name and column? 58 00:04:49,920 --> 00:04:55,500 So right now we're returning a tuple and we saw that we can do tuple unpacking. 59 00:04:55,500 --> 00:04:55,840 Right? 60 00:04:56,010 --> 00:05:06,690 I can say key value equals item and then just print key value if I run this. 61 00:05:08,490 --> 00:05:11,220 You see that I'm able to print these. 62 00:05:12,420 --> 00:05:18,640 But there's actually a shorthand way of being able to do this in here in the for loop. 63 00:05:18,660 --> 00:05:20,400 I can say key. 64 00:05:21,520 --> 00:05:22,150 Value. 65 00:05:23,360 --> 00:05:26,630 And just completely avoid this entire line. 66 00:05:27,640 --> 00:05:28,870 And if I run this. 67 00:05:30,240 --> 00:05:30,820 There you go. 68 00:05:30,990 --> 00:05:37,530 It still works and this, again, is a very common pattern where if you're collecting the items of a 69 00:05:37,530 --> 00:05:42,800 dictionary, you can separate them into key value or you might even see shorthand. 70 00:05:43,080 --> 00:05:44,130 OK, and the. 71 00:05:46,290 --> 00:05:52,770 Again, these can be any variables that you want, you can name them whatever helps your code be more 72 00:05:52,770 --> 00:05:53,250 readable. 73 00:05:54,270 --> 00:06:01,320 By the way, just for fun, let's say, in the last line here, I want to iterate over the number 50. 74 00:06:02,200 --> 00:06:04,840 What do you think will happen then if I click run? 75 00:06:06,230 --> 00:06:11,760 Sorry, Namir Naki, hope I get an error here because I haven't changed. 76 00:06:11,780 --> 00:06:12,950 So let's change these back. 77 00:06:13,810 --> 00:06:18,700 That's not the area I was expecting, the error I was expecting was this. 78 00:06:19,620 --> 00:06:23,850 Type air int object is not iterable. 79 00:06:24,770 --> 00:06:30,770 Now, this area should make sense now, because I've said it over and over, why is it not iterable? 80 00:06:30,920 --> 00:06:35,320 Well, because it's not an object that can be iterated over. 81 00:06:35,480 --> 00:06:41,960 It's not a collection of items because we learned that in Python, these are iterable I'll see in the 82 00:06:41,960 --> 00:06:43,340 next video, Papy.