From the course: Programming Foundations: Data Structures (2023)
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
When to use stacks - Python Tutorial
From the course: Programming Foundations: Data Structures (2023)
When to use stacks
- [Instructor] Stacks are great for programs where you need to keep track of state, just like we saw in our last challenge. Appending, peeking and popping take very little time. In fact, constant time. They're great data structures to use if you're operating on your data in a last in, first out manner. However, if you find yourself needing to index your data structure or get a specific item in the middle, stacks are not the solution. Searching is also not ideal because you would need to pop off everything on the stack and essentially destroy the data structure in order to see each individual item. Overall, a stack really only shows its advantages when you use it in the way it was designed.