๐Ÿงฎ
๐Ÿงฎ Data Structures & Algorithms ยท DS-101 ยท Session 2 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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.

OperationSpeedWhy
Access by index (list[i])InstantJumps straight to the position
Add/remove at the endFastNo shifting needed
Loop over all itemsLinearVisits each once - unavoidable
Find/membership (x in list)SlowMust 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).

๐ŸŽฏ
End = cheap, front = expensive. Adding or removing at the end of a list is fast, but at the front every other element must shuffle along, which is slow for big lists. And checking whether a value is in a list means scanning it. If you're doing lots of front-inserts, a different structure (a deque - next session's territory) fits better; if you're doing lots of membership checks, use a set. Knowing these costs is what 'knowing your data structures' means.

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.

Session 2 of 6 complete

Next up: Dictionaries & Sets: Instant Lookup

Continue to Session 3 โ†’

Related lessons

More in this course

Course home → · Browse all courses →