Stack with deque

EasyCollections Programs · 9 of 20

Write a Python program that uses a deque as a last-in, first-out stack.

Advertisement
Output
back to product -> search

Approach

  1. append and pop from the same end.

Solution

Python
from collections import deque history = deque()for page in ["home", "search", "product"]:    history.append(page)print("back to", history.pop(), "->", history[-1])
Output
back to product -> search

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs

Leave a Comment