Learn computing fundamentals, programming, algorithms, systems, and software engineering.
An array stores items in a single block of contiguous memory, one right after another. Because each item has a fixed, known offset from the start, you can jump straight to any element by its index in constant time — reading or writing a known position is very fast. The cost is that inserting or deleting in the middle shifts every following element over by one, which takes time proportional to the length of the list. A linked list stores items differently: each element (a node) holds a value and a reference to the next node. Nodes can live anywhere in memory. To find the third item you must walk from the head through the first two, so access by index is slow. In return, inserting or deleting a node only requires rewiring a couple of references, once you have found the right spot. In Python the built-in list behaves like a dynamic array: indexing is fast, appending to the end is fast, and inserting at the front or middle shifts elements. This is why you rarely need to hand-write a linked list in Python. Understanding the difference, however, explains a lot about why some operations feel fast and others slow. Choose an array (Python list) when you mostly read by index or append to the end. A linked list is useful when you frequently insert or remove at the front or middle of very large collections and you do not need random access — but in everyday Python, a list is almost always the right starting point.
A data structure is a way of organising data in memory so it can be accessed and updated efficiently. Different structures make different operations fast or slow, so choosing the right one matters.
An array stores elements one after another in a single block of memory. Because each element sits at a known offset from the start, the computer can jump straight to any index.
A linked list stores each value in a node that also points to the next node. Nodes can live anywhere in memory; you follow the pointers to walk the list.
| Array | Linked List | |
|---|---|---|
| Memory | Contiguous block | Scattered nodes |
| Access by index | O(1) — instant | O(n) — must walk |
| Insert / delete | O(n) — shifts elements | O(1) once located |
| Extra memory | None | One pointer per node |
nums[-1] is negative indexing: -1 means the last element, -2 the second last, and so on.
A Python list gives constant-time access to any index, including negative indexes from the end.
a c d
Appending to the end is cheap; inserting at the front shifts every element. Both work, but the cost differs as the list grows.
[1, 2, 3, 4] [0, 1, 2, 3, 4]
Each node holds a value and a reference to the next node. You walk the chain to visit every element in order.
1 2 3