Lesson · 40 min · Free
Stacks and Queues in Python
Stacks and Queues in Python body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 15px; border-radius: 5px; overflow-x: auto; } code { font
Stacks and Queues in Python
Welcome to this lesson on Stacks and Queues in Python, crucial data structures that find numerous applications in computer science, and by extension, in computational tasks within pharmaceutical research. While seemingly simple, understanding their fundamental principles and how to implement them in Python is vital for efficient data management and algorithm design, especially when dealing with sequential processes, task scheduling, or even managing experimental data flow. At an upper-undergraduate level, you should be familiar with basic Python programming concepts. This lesson will focus on how Python's built-in data types can be leveraged to implement these structures and discuss their practical implications.
Understanding Stacks and Queues
Stacks are a type of linear data structure that follow the Last-In, First-Out (LIFO) principle. Imagine a stack of petri dishes: you can only add a new dish to the top, and you can only remove the topmost dish. This behavior makes stacks ideal for scenarios where the most recently added item needs to be processed first. Common applications include function call management (the call stack), undo/redo functionalities, and parsing expressions. In Python, a simple list can effectively be used as a stack. The append() method adds an item to the "top" (end) of the list, and the pop() method removes and returns the item from the "top" (end) of the list. This naturally enforces the LIFO behavior. # Implementing a Stack in Python using a list experimental_samples = [] # Adding samples (pushing onto the stack) experimental_samples.append("Sample_A_Batch_001") experimental_samples.append("Sample_B_Batch_002") experimental_samples.append("Sample_C_Batch_003") print("Current stack of samples:", experimental_samples) # Processing the most recent sample (popping from the stack) processed_sample = experimental_samples.pop() print("Processed sample:", processed_sample) print("Remaining samples:", experimental_samples) processed_sample = experimental_samples.pop() print("Processed sample:", processed_sample) print("Remaining samples:", experimental_samples) # Trying to pop from an empty stack will raise an IndexError # try: # experimental_samples.pop() # except IndexError: # print("Stack is empty!") Queues , on the other hand, are linear data structures that follow the First-In, First-Out (FIFO) principle. Think of a queue at a pharmacy counter: the first person to arrive is the first person to be served. This makes queues suitable for managing tasks in the order they were received, such as processing drug orders, scheduling computational jobs, or simulating patient flow. While a Python list can technically be used as a queue (using append() for enqueue and pop(0) for dequeue), pop(0) is inefficient for large lists because it requires shifting all subsequent elements. For efficient queue implementations, Python's collections module provides the deque (double-ended queue) class, which is optimized for fast appends and pops from both ends. from collections import deque # Implementing a Queue in Python using deque drug_orders = deque() # Adding drug orders (enqueuing) drug_orders.append("Order_Paracetamol_100ct") drug_orders.append("Order_Amoxicillin_500mg") drug_orders.append("Order_Insulin_Pens") print("Current queue of drug orders:", drug_orders) # Processing the oldest order (dequeuing) current_order = drug_orders.popleft() # popleft() removes from the front print("Processing order:", current_order) print("Remaining orders:", drug_orders) current_order = drug_orders.popleft() print("Processing order:", current_order) print("Remaining orders:", drug_orders) # Trying to popleft from an empty deque will raise an IndexError # try: # drug_orders.popleft() # except IndexError: # print("Queue is empty!") In pharmaceutical research, stacks could be used to manage the sequence of operations in a complex synthesis pathway, ensuring that reagents are added and reacted in the correct reverse order of removal. Queues might be used to manage a batch of samples awaiting analysis on a mass spectrometer, ensuring that samples are processed in the order they were submitted to maintain workflow integrity and prevent data mix-ups.
Key Takeaways
Stacks are LIFO (Last-In, First-Out) data structures, best implemented in Python using lists with append() and pop() . Queues are FIFO (First-In, First-Out) data structures, best implemented in Python using collections.deque for efficiency, with append() and popleft() . Understanding these structures is fundamental for designing efficient algorithms and managing sequential data flows. Applications in pharma include managing experimental protocols, task scheduling, and data processing pipelines.
Practice Exercise
Imagine you are developing a system to manage patient samples for a clinical trial. Each sample needs to undergo a series of tests. Design a Python program that simulates the handling of these samples. Implement a queue to hold samples waiting for the first stage of testing. Once a sample completes the first stage, it is added to a stack representing samples ready for a final, critical analysis (which must be performed on the most recently completed sample first). Write code to add 3 patient samples to the queue, process them through the first stage (dequeue from queue, then enqueue to stack), and then process them from the final analysis stack. Print the state of both the queue and the stack at each significant step.
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →