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

Efficiency

00:00 Quality code is efficient, meaning it uses appropriate algorithms and data structures to run fast without wasting memory. Let’s look at an example. You have a list comprehension that calculates the sum of the squares of all numbers from zero to 999,999 and then stores them in total.

00:23 Okay, this is functional code. It’s also readable. Do you think this code has an efficiency issue? Well, it does. Why, you might ask? The main reason is that this code creates a full list of one million squared numbers in memory before passing it to sum.

00:42 If you wanted it to be more efficient, you could use a generator expression. Here you get the exact same calculation, but you’re not building the entire list first.

00:53 You just produce the squared numbers one at a time, and you save memory here.

00:59 Let’s look at more examples. Your goal here is to find the price of a specific item inside a store’s catalog. You’re doing it in two different ways.

01:09 First in an inefficient way and then in an efficient way. Let’s look at the first one. You have a function called get_item_price. You’re getting target_item and price_list.

01:21 Then you’re using a for loop to loop through the list one item at a time until you find the target item. And I mean, it does work. For example, if your catalog maybe has 10, 20, or even 100 items, that’s fine. But if your catalog has, say, one million items, it’s going to be very brutally slow. And if you’re interested in knowing what Big O notation is, the first approach is O(N), meaning the time grows as the list grows.

01:53 Now, what if you use a dictionary here instead of a for loop? A dictionary lets you look up a value directly by its key. So instead of checking every item one by one, Python just goes straight to the item that you want. This approach is usually O(1), meaning the lookup time stays roughly constant even as the catalog grows.

02:16 So both versions are technically functional, but the dictionary version is more efficient because it uses a better data structure for lookup. As a rule of thumb, prefer Python’s built-in data structures and methods when they clearly fit the problem.

02:32 They’re usually more efficient, they’re designed that way.

02:35 Now it’s your turn. Look at this code block. You have a list of numbers, you have a total variable, and then a for loop that goes through every number in the numbers list and adds it to total. How could you make this more efficient?

02:50 Hint: you can use a built-in function instead. Think about it and then comment your answer below.

02:57 Congrats, you just wrapped up efficiency. You now understand that using appropriate algorithms and data structures help your code run fast without wasting memory.

03:08 Next up is reusability.

Become a Member to join the conversation.