Lists & Arrays: Ordered Data
The list is the workhorse data structure โ an ordered sequence you can index, slice and grow. Knowing what it's good and bad at is fundamental.
The list (called an array in many languages) is the most-used data structure of all: an ordered collection you access by position. You met lists in Python Fundamentals; here we look at them as a data structure - what they're genuinely good at, what's surprisingly slow, and when a list is the right tool versus when it isn't.
What lists are great at
Lists shine when order matters and you access items by position or process them in sequence.
| Operation | Speed | Why |
|---|---|---|
| Access by index (list[i]) | Instant | Jumps straight to the position |
| Add/remove at the end | Fast | No shifting needed |
| Loop over all items | Linear | Visits each once - unavoidable |
| Find/membership (x in list) | Slow | Must scan until found |
Lists are perfect for ordered sequences you index or iterate. The one thing to watch: searching a list for a value is a scan, so it gets slow for big lists (that's what sets/dicts are for).
Watch the slow spots
Two operations are quietly expensive on big lists: searching for a value, and inserting/removing at the front (everything after has to shift).
Use lists well
- Great for ordered data, indexing, iterating, and appending to the end.
- Slow for searching by value and inserting/removing at the front.
- Slicing (list[a:b]) is handy but copies - fine for small ranges.
- Reach elsewhere when you need fast lookups (set/dict) or front-ops (deque).
๐ฏ Key takeaways
- A list is an ordered sequence you access by position - the workhorse structure.
- Fast: indexing, appending to the end, iterating. Slow: searching by value, front inserts/removes.
- Searching a list is a scan - use a set/dict when you do lots of membership checks.
- Front operations shift everything - use a deque when you need fast add/remove at both ends.
Frequently asked
Why is finding an item in a list slow but indexing fast?
Because they're fundamentally different operations. Indexing (list[i]) is fast because a list stores its elements in order and can jump straight to position i by simple calculation - it doesn't look at any other elements. Finding a value (x in list) is slow because the list has no idea where x might be, so it must check elements one by one from the start until it finds a match or reaches the end - a scan whose time grows with the list's length. This is exactly why sets and dictionaries exist: they organise data so that finding a value is near-instant, at the cost of not preserving position-based access the way a list does. Match the structure to whether you access by position or by value.
When should I use a list versus something else?
Use a list when order matters and you work with items by position or in sequence - iterating over things, keeping a history, a queue of steps, results in order. Move to something else when the list's weak spots bite: use a set or dictionary when you frequently check membership or look things up by key (their fast lookups beat a list's scan), and use a deque (double-ended queue) when you frequently add or remove at the front (lists shift everything, deques don't). The skill is recognising the access pattern - by position/order (list), by value/key (set/dict), or heavy front-operations (deque) - and picking accordingly. Lists are a great default; you switch when a specific weakness matters.
Related lessons
Why Data Structures Matter
How you store data decides how easy - and how fast - everything else becomes. Choosing theโฆ
Data Structures & AlgorithmsDictionaries & Sets: Instant Lookup
Dictionaries and sets are the secret weapons of fast code. Thanks to hashing, they find thโฆ
Data Structures & AlgorithmsStacks, Queues & Beyond
Some structures aren't about storage but about *order of access* โ stacks (last-in-first-oโฆ