From the course: Python Essential Training

Dictionary comprehensions - Python Tutorial

From the course: Python Essential Training

Dictionary comprehensions

- [Instructor] Dictionary comprehensions are used to generate dictionaries from iterable structures. Just like list comprehensions create a new list, a dictionary comprehension creates a new dictionary. I'll show you a basic example. So here we have a list of tuples that we'll be using as our key value pairs. Remember, tuples work just like lists, except that you can't alter the value of tuples once they've been declared. And we can create a dictionary from this list of tuples like this, animals equals item 0 colon item 1 for item in animalList. And there we see our dictionary. There's this familiar for item in list statement exactly the same in both list and dictionary comprehensions, but now we need to define both the key and the value separated by a colon instead of just a single value like we did for the list comprehension. And of course, this dictionary comprehension is surrounded with the curly braces, just like the list comprehension was surrounded by the square brackets. Now, there's a more elegant way to write this dictionary comprehension as well. Let's check that out. So we can actually say key is equal to value for key comma value in animalList. So what kind of sorcery is happening here? Well, whatever is between the for and the in in this statement is what each tuple of animalList is getting assigned to. So remember when we saw earlier when we were discussing tuples and sets, Python allows you to unpack values into multiple variables as long as the number of variables you're assigning the values to matches the elements in this data structure. So we have two items in each tuple here and we're unpacking them into two key value variables here that we can use and this just makes it a little bit cleaner. So we can do something like this, but if we try to unpack it into otherItem, you can see we'd get not enough values to unpack, expected three, but actually got two when we iterated through this list. So that's the dictionary comprehension. Pretty straightforward. Well, what if we want to take our animal dictionary and turn it back into a list? Let's look at the dictionary function called items. Remember we have animals.keys and animals.values. There's actually animals.items and items returns a dict_items object containing a list of key value pairs. So we can turn it back into something that looks like this animalList that we started with just by doing list animals.items. And there you go. But let's say we wanted to get a little more creative. We want each item in our list to have a structure different than the one we started with. Well, we could write a list comprehension and let's make it key value, or let's call this name value for key comma value in animals.items. This returns a list of dictionary objects with the original keys and values under the letter and name fields in each dictionary in the list. So you can see how powerful both dictionary and list comprehensions are for processing and formatting data in Python.

Contents