Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

How to Use itertools With Dictionaries

00:00 All right! So, I’m going to be talking in this video about itertools. And itertools is really an amazing Python library which allows you to just do an incredible number of iterative tasks on any kind of iterable collection.

00:15 But what we’re going to focus on today is how itertools can be used with dictionaries and how you can do some cool stuff with itertools and dictionaries.

00:23 So with that in mind, let’s move over to the terminal. And we’re going to create a little dictionary here, which is going to be familiar to those of you who’ve been watching this series.

00:31 It’s going to be just a little prices dictionary. And as you might notice, I don’t really have a good idea of what these prices should be because they’re changing every time, but that’s okay.

00:42 We have a dictionary of prices—from fruits to their prices in cents. And the first thing from itertools that we’re going to take a look at is the cycle() function.

00:53 And this is pretty cool because what it does is it allows us to iterate repeatedly through an iterable—in this case, a dictionary—over and over and over until we want to stop.

01:04 And so it prevents you from having to…Say you want to go through this list, you know, 10 times and do a certain set of things each time. Well, you don’t want to write 10 for loops. That would just be silly.

01:15 And so you can use this cycle function to go through this dictionary as many times as you want and just continue on. So, for example, let’s say something like this.

01:26 And of course, we need to have some way to make it stop. So I’m going to say just num_items = 10, and then I’m going to say for item in cycle(prices.items()):.

01:37 Sorry. That’s a little bit better. I’m going to say print(item). Well first, okay. Sorry, I’m going to say—hello? if num_items, this is going to be our stop condition, 0, then break.

01:53 And then otherwise, we’re going to say num_items -= 1, and then we’ll just print the item. Just so we can get a sense of how this works.

02:01 This isn’t anything that you would actually do in a real application, of course. So, there we go. As you can see, we’re iterating through in order and over and over until we reach our stop condition, which in this case was just that there are 10 items in the total number of iterations, right? So that’s how you use cycle(). And of course, if I hadn’t included this breakout condition, we would just keep cycling forever, but that would be boring to watch so I didn’t want to do that.

02:28 Another cool thing from itertools is the chain() function. And so, what’s fun about that, if you remember from ChainMapwhich we did in the last video—this is a very similar idea, except it doesn’t create an object which has these chained properties.

02:44 It just allows you to iterate through a chained version of the things. So for example, let’s define a dictionary called fruits, which is just going to go 'apple' goes to 10, 'banana' goes to 20.

02:59 This is maybe just our quantity of fruits. And then we’ll have one called veggies,

03:03 which is going to be 'carrot',

03:09 it’s just going to be 5 carrots, and then we’ll have a bunch of potatoes—45. So, we have fruits and veggies. If you remember for a ChainMap, we would have defined a ChainMap with these fruits and veggies, and then we would have iterated through it. But instead what you can do is you can say for item in chain(fruits.items(), veggies.items()):

03:32 you can just, you know, print the item. And you can do all sorts of things with it, but we’re just going to print it. So as you can see, we’ve chained up these two dictionaries and allowed us to iterate through all of the items in both of them without having to use any unwieldy for loop constructions. We just looped through this chained thing.

03:51 You can also use .keys(), .values(), whatever else you want to do—anything that’s iterable. You could even chain together the two dictionaries themselves, but then it would be for key in the chain of the two dictionaries.

04:02 And you would have to somehow figure out which key was in which dictionary, so I don’t recommend that, actually. I recommend using .items() or .keys() and .values() if need be.

04:12 So, itertools gives us just some new options for iterating through dictionaries and their items. It’s pretty fun, and I would encourage you to check out the documentation of itertools for more on what it can do, ‘cause it’s a really amazing library. Thanks!

viraj7k on April 10, 2020

hello sir, can we use dictionary comprehensions here?

henrytirla on Nov. 14, 2020

Dope Lectures

Become a Member to join the conversation.