Stop 05 · Built-in Data StructuresTopics · 9/9

Choosing the Right Data Structure

Part of the Python Roadmap

In short

The same problem can feel painful with a list and effortless with a dict or set. Picking the right container is one of the quickest ways to write shorter, faster code, and interviewers love to ask about it.

Use a list for ordered items you change, a tuple for fixed records, a dict for lookups by key and a set for uniqueness and fast membership checks. The right choice makes code shorter and faster.

The jaadu stands at a kitchen counter deciding between a round spice box, a shopping bag, labelled jars and a fruit bowl.

Think of it like this

Containers in a kitchen

A masala dabba keeps spices in fixed slots you never rearrange (tuple). A shopping bag holds items in the order you picked them and you keep adding more (list). Labelled jars let you grab sugar by name (dict). A bowl of unique fruits makes sure you never take two of the same (set). Each one fits a different job.

Watch it happen

Which container should I use?

What do you need?
Look up by name?
dict
Only unique items?
set
Ordered sequence?
list (changes)
tuple (fixed)

1/5Start from what the code has to do with the data, not from habit.

The big ideas

What you need to know

Speed of 'in'

Checking x in a list looks at items one by one. In a set or dict keys it's almost instant, which matters with large data.

Order and position

Need item number 5 or a sorted order? Lists and tuples have positions. Sets don't.

Will it change?

Data that shouldn't change belongs in a tuple. It also documents your intent for anyone reading the code.

Combine them

Real data is often a list of dicts (rows of records) or a dict of lists (groups). Mixing structures is normal.

See it in code

Walk through the code

python
1# list: ordered queue of tasks that grows2tasks = ["pay electricity bill", "book train ticket"]3tasks.append("call plumber")4 5# tuple: one fixed record6location = ("Jaipur", 26.91, 75.79)7 8# dict: look up stock by product code9stock = {"P101": 25, "P102": 0, "P103": 12}10print(stock["P103"])                 # 1211 12# set: who has already voted?13voted = {"V001", "V007"}14voter = "V007"15if voter in voted:16    print("Already voted")17 18# list of dicts: rows of records19orders = [20    {"id": 1, "city": "Pune", "amount": 450},21    {"id": 2, "city": "Surat", "amount": 900},22]23total = sum(o["amount"] for o in orders)24print(total)                          # 1350

1/5Tasks have an order and keep growing, so a list fits.

One small app, four containers, each chosen for what the code does with it.

Save yourself the pain

Common mistakes

Oops

Searching a list of 1 lakh ids with if user_id in id_list inside a loop.

Do this instead

Convert it once with id_set = set(id_list). Each check becomes near-instant.

Oops

Keeping parallel lists like names = [...] and marks = [...] and matching them by index.

Do this instead

Use a dict {name: mark} or a list of tuples so related data stays together.

Your mission

Pick the container

~20 min

Quick check

You need to check thousands of times whether a coupon code has already been used. Best choice?

Go deeper

  1. 1For five small problems, write down which structure you would use and why.
  2. 2Time a membership check on a big list versus a set.
  3. 3Model a train timetable and decide which parts are lists, tuples or dicts.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge