Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Putting Python's Deque Into Action
00:00
In this lesson, you’ll see how to apply deques to real-world problems. Let’s imagine that you’re building an application that scrapes data from search engines and social media sites.
00:10
At some point, you need to keep track of the last three sites that your application requested data from. To solve this problem, you can use a deque with maxlen of three items.
00:22
First, create a collection of sites. This tuple contains the sites that the application has visited. pages keeps a list of the last three sites that your application visited. You can verify the maximum capacity by doing pages.maxlen. Now add each site to the left side of the deque.
00:51
At this point, the deque is full.
00:55
So adding a new site to the end of the deque automatically discards the site at the opposite end. This behavior keeps your list up to date with the last three sites you need.
01:07
Notice that google.com was dropped from the deque to make room to add facebook.com. This makes it a simple and efficient way to maintain a fixed-size history.
01:18
In the next lesson, let’s recap what you’ve learned about deque and what’s next in your learning journey.
Become a Member to join the conversation.