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

Following Best Practices

00:00 In the previous lesson, I showed you more examples of dictionary comprehensions. In this lesson, I’ll talk a bit about best practices and things you should avoid.

00:09 There are two things that might make you want to use a dictionary comprehension. They take up fewer lines of code than their for loop cousins, and they might be more readable as they look like a single sentence or idea in your code.

00:21 Of course, readability is a judgment call and overly complicated comprehensions might actually defeat this advantage and make it worse, not better. Generally speaking, comprehensions are faster than their for loop equivalent, but the important part of that statement is generally speaking. When it comes to performance, you should always test—your specific case may not be faster or the difference in performance might be minuscule and not worth it.

00:46 One other thing to keep in mind when using comprehensions is you’re creating a new dictionary. This means more memory getting used. For large datasets, that might be problematic.

00:56 Everything in coding is a trade-off. For large datasets, you might consider a generator expression instead. This is a way of dynamically producing the next value in a series, meaning that for calculated values, you’re not taking up all the extra memory.

01:11 The downside is they aren’t indexable and aren’t dictionaries—you only get one item at a time. The summary of this course contains a list of other sources of information.

01:20 I’ll include more on generator expressions there.

01:25 Sometimes it’s easier to understand the best choices by talking about some bad choices, so let’s do that. I kind of hinted at this one when talking about the readability benefit.

01:34 If your comprehension is getting unwieldy because it has a lot of extra parts, or if you’re finding you need to nest comprehensions in your comprehensions, you might be better off putting it in a for loop, so the code is easier to understand.

01:48 The example on the screen here is creating a price dictionary, but getting the price from two different places, both the original price dict and a discount list.

01:56 The resulting comprehension has an inline if-else ternary clause, making the comprehension messy to understand. I’m old school and still stick with 80 character line lengths in my code.

02:08 A good rule of thumb is if your comprehension doesn’t fit on a single line, it probably should be broken up into a for loop. Even if you don’t stick to 80 characters like I do, if you’re having to scroll your IDE to read the comprehension, it’s probably too much.

02:25 Just because you’ve got a single line comprehension doesn’t mean it’s going to be speedy. Creating dictionaries to store results is almost always a memory versus computation time trade-off.

02:35 This isn’t just specific to comprehensions, but if the computation is expensive, then sticking it in a dict might not be the right answer. Say you’ve got thousands of items to put in a dict and an expensive calculation for each.

02:47 You likely need to be using each of those thousands of items many times before you get a performance trade-off. Calculating values on the fly might be a better approach.

02:57 You can mix and match as well by using a dictionary as a cache. If the item isn’t in the dict, calculate it and then put it in the dict so it can be reused.

03:06 The other thing to consider with comprehensions is the scope of the values in the comprehension. They stay inside the comprehension. This is unlike a for loop where the variable in the declarative statement is available outside the block.

03:20 If you need the iteration target after iterating, you must use a for loop, you can’t use a comprehension. In fact, this is one of the sources of the performance boosts I was talking about.

03:30 The compiler isn’t putting any permanent values into scope and thus can do some optimizations. And there you have it. The next lesson is the summary where I’ll review the course and point you at other possible resources.

Become a Member to join the conversation.