Go beyond lists with Stack as LifoQueue(), Queue(), and Deque() in Python to tackle tasks with efficiency — working code examples and illustrations within. Pancakes not included.

About This Story

I recently learned about a few cool tools to work with data that go beyond the standard Python list and I think they are fantastic! To be sure, these topics are well-worn paths, but would still like to share a few examples that helped me get my head around Stack(), Queue(), and Deque() in Python.

In This Story

  1. Lists: Review a familiar way to collect stuff in one place
  2. Stacks: Looks like a list, but with a Last-in, First-out implementation
  3. Queues: Looks like a stack, but with a First-in, First-out implementation
  4. Deques: A double-ended Queue that solves a big problem with lists

The List

In Python, the list is the most versatile compound data type. For example, after figuring out the ‘hello world’ bit, managing a list is among the first things we will ever learn. Moreover, lists are so useful that we will eventually try to use them for everything. However, despite the amazing things we can do with a list, eventually, we need some other friends like tuples and sets. But still, there are some very specific jobs out there that require specific tools, and that’s where our pals Stack(), Queue(), and Deck() come into play.

Iterating Over a List

With the list, we have a very manageable way to organize data. Of the many things we can do, if we have a list of boxes we might print a list with iteration.

Example of iteration to print a list of five boxes from “Box A” to “Box E.” Original list in black, printed list in gray.

With iteration, we can print a list of objects. Illustration from the author.

As illustrated above, we can use a simple loop to print a list of boxes with iteration in the code below. Important to note, with iteration, we start with the first thing in the list and then move onto the next thing until we’re at the end.

## Example 1: Print a list with Iterationlst_of_stuff = ['Box A', 'Box B', 'Box C', 'Box D', 'Box E']for i in lst_of_stuff:
  print(i)'''
Output:Box A
Box B
Box C
Box D
Box E
'''

Manipulate a List with Append() and Pop()

Beyond a simple print job, things start to get interesting when we think about editing the list with append() and pop(). In Example #1 above, we only printed data from the list of boxes, but did not edit the list. However, with append() and pop(), we can add and remove boxes to and from the right side of the list.

As I read from a few places, they say a list grows right and shrinks left.

Example of editing a list with append() and pop(). Notice how they grow or shrink from the end or the right side of the list.

Add or remove data from the right side of the list with append() and pop(). Illustration from the author.

In Example #2, it is almost as if our humble row of boxes are in a narrow garage — the only way to move stuff around is to pick up whatever box is at the end of the row. In the code example below, we can see this analogy in action by popping off Box E and by appending a new Box F.

## Example 2: Edit a list with pop and appendlst_of_stuff = ['Box A', 'Box B', 'Box C', 'Box D', 'Box E']## pop off the last thing in the listprint(lst_of_stuff.pop())## >>> Box E## append something new to the end of the listlst_of_stuff.append('Box F')
print(lst_of_stuff)## >>> ['Box A', 'Box B', 'Box C', 'Box D', 'Box F']

#python #developer

Get Started with Stacks and Queues in Python
1.75 GEEK