Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Transforming Dictionaries

00:00 In the previous lesson, I showed examples that turned interables into dictionaries. In this lesson, I’ll show more examples this time, doing transformations from one dictionary to another.

00:10 Sometimes you have an existing dictionary, but need a modified version of the data. With comprehensions, you can do this by taking advantage of the iterable methods on the dictionary itself.

00:21 Let’s open the REPL and look at two examples. Consider our dictionary of fruit prices from a few lessons ago. Now, let’s say you want to put everything on sale, taking 5% off.

00:33 A comprehension needs something to iterate over. Luckily, the dict class has methods just for that. The items() method returns an iterable containing tuples, where each tuple contains the key and corresponding value. To create our sale price dictionary, you can iterate over items and do the math on the original price.

01:03 Starting on the right-hand side here, I’m iterating over the key-value pairs of the original dict using the items() method. I then unpack those returned tuples into item and price, and then on the left, I create a key using the item and the new sales price knocking 5% off.

01:23 Consider a dictionary that contains the name of a part and its corresponding part number.

01:37 For certain kinds of situations, you might want the reverse mapping of this with the part number being the key and the name being the value. To do this, you can iterate over the original dictionary using items() once more, and simply swap the key-value pair.

01:52 There

01:59 are a few things to consider here. First, dictionary keys have to be hashable. Values don’t, so there are cases where this won’t work because your value might not be able to be a key.

02:10 Second, keys in a dictionary are unique, but values don’t have to be. If two items in the dictionary had the same value, the last one to be iterated over will stomp on the original, leaving you with a missing key-value in the result.

02:23 Our example with part numbers doesn’t have that case because part numbers should be unique, but these are things you should think about before using this transformation.

02:32 In earlier lessons, you saw how you could pass tuples to the dict constructor, and you also saw how to use zip() to pair two sets of things.

02:40 You can combine those two ideas as an alternate way of doing a reverse mapping like I just showed you. To do this trick, you have to be able to iterate over both keys and the values in the dictionary.

02:54 The values() method provides an iterator over the values,

03:00 while the keys() method provides an iterator over the keys. By combining these two with a call to zip(),

03:11 you can construct the same kind of reverse mapped dictionary as you did with the comprehension. Before dict comprehensions were added to Python, this was the way to create a reverse mapping.

03:22 I actually prefer the comprehension in this case because it feels more straightforward to me, you kind of have a better idea of what it’s doing. That’s a personal preference though.

03:33 In the next lesson, I’ll outline some best practices for dictionary comprehensions.

Become a Member to join the conversation.