Learn

In Python, lists are one of the most common containers we use to work with data. Unfortunately, there are certain situations where they perform poorly.

Let’s imagine a situation where we are processing a large document containing bug reports for an application. In order to prioritize the most important bugs, we want any normal bug reports to be appended to the end of the list and higher priority bugs to be at the front of the list (kind of like a priority list). As we fix the bugs, they can be removed from the front of the list.

The program below is an example of what our implementation might look like using lists. Take some time to understand what the code is doing.

bug_data = [] loaded_bug_reports = get_all_bug_reports() for bug in loaded_bug_reports: if bug['priority'] == 'high': # A list uses the insert method to append to the front bug_data.insert(0, bug) else: bug_data.append(bug) # A list must provide an index to pop next_bug_to_fix = bug_data.pop(0)

The problem with this implementation is that lists are not optimized for appending and popping large amounts of data, although they are great at accessing data at any index which you provide.

To solve this problem, we can use deque containers. These are similar to lists, but they are optimized for appending and popping to the front and back, rather than having optimized accessing. Because of this, they are great for working with data where you don’t need to access elements in the middle very often or at all.

Let’s observe our same program but implemented with a deque:

from collections import deque bug_data = deque() loaded_bug_reports = get_all_bug_reports() for bug in loaded_bug_reports: if bug['priority'] == 'high': # With a deque, we can append to the front directly bug_data.appendleft(bug) else: bug_data.append(bug) # With a deque, we can pop from the front directly next_bug_to_fix = bug_data.popleft()

More information about the deque container can be found in the Python Documentation

Now let’s try working with a larger amount of data where a deque is more beneficial!

Instructions

1.

We need to order a large number of supplies for our clothing company. A CSV file has been given to us by our team which lists each of the material’s names, the quantity, and the importance of that material.

A function is provided which reads the CSV file and returns a list of lists containing the data in the format of [[material, number_of_pallets, importance], [material, number_of_pallets, importance], ...].

We’ll need to use a deque to separate the data into important and non-important items using our new trusty deque collection. First, create an empty deque called supplies_deque.

2.

Using a for loop, read each item from csv_data. On each iteration, if the item is marked as important, append it to the front of supplies_deque, otherwise append it to the end.

3.

Your accountant let you know that you have enough of a budget to order 25 important materials and 10 unimportant materials.

For this step, create a new deque called ordered_important_supplies. Remove the 25 important items from your supplies_deque and append them to ordered_important_supplies.

4.

Now that you have completed the orders for the 25 important items, repeat the same process for 10 unimportant items.

Create a new deque called ordered_unimportant_supplies. Remove 10 low important items from your supplies_deque and append them to ordered_unimportant_supplies.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?