WEBVTT 1 00:00:00.750 --> 00:00:02.550 line:15% Hi guys, and welcome back! 2 00:00:02.550 --> 00:00:04.000 In this video, we're going to learn 3 00:00:04.000 --> 00:00:06.793 line:15% about iterating over dictionaries. 4 00:00:07.720 --> 00:00:10.900 Just as we iterated over lists or tuples 5 00:00:10.900 --> 00:00:14.130 with the for loop, we can iterate over dictionaries. 6 00:00:14.130 --> 00:00:17.160 Imagine you've got a dictionary of your friend names 7 00:00:17.160 --> 00:00:20.770 to their ages, such that you can access friend_ages Rolf 8 00:00:20.770 --> 00:00:25.143 to get 25, or friend_ages Anne to get 37. 9 00:00:26.740 --> 00:00:28.823 You can do for name in friend_ages. 10 00:00:29.670 --> 00:00:32.160 And again, this is a variable that we can name 11 00:00:32.160 --> 00:00:35.383 whatever we want, but I'm calling it name at this point. 12 00:00:36.310 --> 00:00:37.680 And you can print name. 13 00:00:37.680 --> 00:00:40.130 What happens when you do this is, 14 00:00:40.130 --> 00:00:43.810 you're iterating over the dictionary's keys. 15 00:00:43.810 --> 00:00:48.810 So what you would get here is Rolf, Anne, Charlie, and Bob. 16 00:00:49.330 --> 00:00:53.610 The values, 25, 37, 31, 22, will be ignored 17 00:00:53.610 --> 00:00:56.380 and won't be present anywhere in here. 18 00:00:56.380 --> 00:00:59.903 So if we run this, you'll see that you get the keys back. 19 00:01:00.880 --> 00:01:04.810 If you wanted the values, you could iterate 20 00:01:04.810 --> 00:01:08.463 over friend_ages.values. 21 00:01:10.370 --> 00:01:12.520 And then if we run this, now you'll get 22 00:01:12.520 --> 00:01:17.260 25, 37, 31, 22, so just the ages. 23 00:01:17.260 --> 00:01:20.683 Name, age in friend_ages.items. 24 00:01:25.340 --> 00:01:26.640 And what you're gonna get now 25 00:01:26.640 --> 00:01:29.830 is this tuple of names and ages 26 00:01:29.830 --> 00:01:33.180 that you can de-structure into two variables directly here 27 00:01:33.180 --> 00:01:37.400 in the for loop, and you'll get the name and the age. 28 00:01:37.400 --> 00:01:39.100 This here is the most common way 29 00:01:39.100 --> 00:01:41.450 of iterating over a dictionary. 30 00:01:41.450 --> 00:01:44.680 Usually, you'll either iterate over the keys only 31 00:01:44.680 --> 00:01:48.010 or you will iterate over the key and the value 32 00:01:48.010 --> 00:01:52.720 by using .items on your dictionary variable. 33 00:01:52.720 --> 00:01:54.770 This is great because it allows you to get 34 00:01:54.770 --> 00:01:57.840 both the key, in case you want to show the key information 35 00:01:57.840 --> 00:02:00.570 when you're printing it out, and also the value. 36 00:02:00.570 --> 00:02:03.050 Remember that if this value is a list, 37 00:02:03.050 --> 00:02:05.360 or another dictionary, or a tuple, 38 00:02:05.360 --> 00:02:07.550 you will still get it in here, just the entire 39 00:02:07.550 --> 00:02:09.620 list or dictionary or tuple. 40 00:02:09.620 --> 00:02:12.790 So anything that is here will be age 41 00:02:12.790 --> 00:02:15.470 and the key will be name. 42 00:02:15.470 --> 00:02:17.590 So there's iteration over dictionaries, 43 00:02:17.590 --> 00:02:20.770 and that's why I introduced to you tuple de-structuring 44 00:02:20.770 --> 00:02:22.570 earlier on in the course. 45 00:02:22.570 --> 00:02:24.000 Thanks for joining me in this video, 46 00:02:24.000 --> 00:02:25.650 and I'll see you in the next one.