From the course: Programming Foundations: Data Structures
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Use a list as a stack - Python Tutorial
From the course: Programming Foundations: Data Structures
Use a list as a stack
- How can we use stacks and programming? Well, we could use the stack data structure to represent a deck in a card game. In the game, the deck of cards would be shuffled so that a series of cards are stacked on top of each other. After assembling the deck, players would each draw a card from the top of the stack. Or we could say the stack would pop off the cards. Now how would this work in code? Some languages give you stack functionality outright. But with others, you have to use the tools given from the language, to implement your own stack. Stacks are essentially an ordered list with a specific way of adding and removing items. You can only add and remove from the top. With this in mind, we can use a list in Python that acts as a stack, to implement this functionality. Let's jump in and check it out. To create a stack of cards, we'll create an empty list that represents our stack. Each element in the list will represent…