Data Structures Basics: Arrays, Lists and Stacks
Why organisation of data matters
Imagine a library with books thrown in a pile versus a library with labelled shelves. Finding a book is the same job — wildly different effort. A data structure is a way of organising data so operations like search, insert and delete are efficient. Interviews and real systems both revolve around choosing the right one.
Arrays — the numbered row of boxes
An array stores items side by side, each with an index starting at 0:
marks = [80, 75, 90, 60]
print(marks[2]) # 90 — instant access by indexStrength: reaching any position instantly. Weakness: inserting in the middle means shifting everything after it, and fixed-size arrays cannot grow.
Lists — the flexible chain
Python’s list behaves like a smarter, growable array:
tasks = ["study", "gym"]
tasks.append("project") # grows easily
tasks.remove("gym") # removes an itemFor most beginner programs, lists are the default choice: ordered, changeable, and packed with useful methods.
Stacks — last in, first out
A stack is a pile of plates: you add and remove from the top only.
stack = []
stack.append("page1") # push
stack.append("page2")
top = stack.pop() # removes page2 firstYour browser’s Back button is a stack of pages; the Undo feature in editors is a stack of actions. Wherever “go back to the most recent thing” appears, a stack is hiding.
Queues — first in, first out
A queue is a line at a ticket counter: the first to arrive is the first served. Printers use queues for documents; apps use them for tasks waiting their turn. Stack = pile, queue = line — that picture alone answers many exam questions.
Practice task (45 minutes)
Simulate a browser: keep a stack list, “visit” five pages by appending their names, then press Back three times using pop(), printing the current page after each step. Then write, in one sentence each, whether a printer, an undo button, and a playlist should use a stack or a queue — and why.
Arrays give instant access by index, lists flex and grow, stacks reverse order (LIFO), queues preserve it (FIFO). Choosing the structure is choosing the behaviour.