Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Think in algorithms
EXPERIMENT 58 / 101

Pick the shape for the job.

Choose between indexed access, scanning, and building a membership structure.

ADVANCED LAB
BY NAVAN SAMI◷ 9 MIN READPYTHON 3

01 The idea

Python lists behave like dynamic arrays of references: indexed access is constant time, appending is amortised constant time, and inserting at the front shifts existing references. Sets and dictionaries use hash tables to support expected constant-time membership or key lookup. Choose based on operations and semantics, not the collection with the fanciest name.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

arrays_and_hash_lookup.py
tasks = ["Build", "Test", "Review", "Build"]
lookup = set(tasks)
print(tasks[1])
print("Review" in lookup)
print(len(tasks), len(lookup))
↳ EXPECTED OUTPUT
Test
True
4 3

03 Under the hood

  1. 1

    The list preserves order and duplicates, which can matter to a schedule. The set removes duplicates and is not a positional sequence.

  2. 2

    Building lookup costs work proportional to the input size. It becomes attractive when many membership checks follow.

  3. 3

    If you need a relationship from a name to a record, use a dictionary rather than parallel lists and repeated scans. Be explicit about what happens when keys repeat.

04 Your turn, builder

A SMALL MISSION

Build a name-to-duration lookup from task pairs and retrieve the duration of Test.

Reveal one possible solution +
one_possible_solution.py
tasks = [("Build", 40), ("Test", 20)]
lookup = dict(tasks)
print(lookup["Test"])

05 Check your signal

QUICK SIGNAL CHECK

What is lost when converting a list to a set?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.