Lists and dicts
list<T> is a Scratch list, with a type on the elements.
public scores: list<num> = [3, 1, 4, 1, 5];public names: list<str> = ["Ember", "Splash"];public empty: list<num> = [];A trailing comma after the last element is allowed, in list and dict literals alike:
public names: list<str> = [ "Ember", "Splash",];Indexing
Section titled “Indexing”Scratch lists are 1-indexed, and so are Katnip’s.
private first: num = scores[1];scores[2] = scores[2] + 1;scores[2] += 1; # compound assignment through an indexMethods
Section titled “Methods”scores.add(7); # appendscores.clear(); # delete allprivate n: num = scores.length();private has: bool = scores.contains(4);private at: num = scores.indexOf(5); # 0 if absentscores.show(); # show the list monitorscores.hide();Every method also has a namespace form — list.contains(scores, 4) — but it does not build
today; codegen looks for a variable named list. Use the receiver form.
Iterating
Section titled “Iterating”for (s, scores) { total += s;}dict<K, V> has no Scratch equivalent. Katnip backs each dict with two parallel Scratch
lists — for stock, they are stock_keys and stock_vals — and keeps their indices in
step.
public stock: dict<str, num> = {"apple": 2, "banana": 5};Reading and writing
Section titled “Reading and writing”private apples: num = stock["apple"];stock["cherry"] = 7; # key missing → appendedstock["apple"] = 3; # key present → replaced in placestock["apple"] += 1;A read resolves the key column to an index, then reads the value column at that index.
Iterating
Section titled “Iterating”for ((name, count), stock) { report(name, count);}The (name, count) tuple pattern walks both columns together.
Where a list can be declared
Section titled “Where a list can be declared”Top level, sprite level, and inside a script or procedure all lower. The difference is when the contents arrive: a top-level list with all-literal contents is baked into the project file, while one declared inside a script is cleared and refilled at that point on every run.
events.onFlag() { private xs: list<num> = [1, 2, 3]; # clear, then three `add` blocks, right here looks.say(f"{xs[1]}");}There is still one Scratch list behind it, shared by every run of that script — Scratch has no local lists, so a declaration inside a body is a placement, not a scope.
How literals reach the project
Section titled “How literals reach the project”If every element is a literal, the contents are baked into the project file and are present the moment the project loads:
public scores: list<num> = [3, 1, 4, 1, 5];If any element needs a block to compute, the whole list is instead rebuilt by a green-flag script:
public roster: list<num> = [1, double(4), 9];That distinction matters when another green-flag script reads the list — Scratch does not order concurrent scripts for you. If you depend on it, broadcast after the rebuild rather than racing it.
Loading a list or dict from a file
Section titled “Loading a list or dict from a file”import("./file") with a .csv or .json path reads the file at compile time and acts as
if you had typed the literal yourself. The path is relative to the source file, and a missing
or malformed file is an error reported at the path.
private levels: list<num> = import("./levels.csv");private scores: dict<str, num> = import("./scores.json");private names = import("./names.csv");| File | Must be | Becomes |
|---|---|---|
.csv |
a single row or a single column | a list |
.json |
a flat array | a list |
.json |
a flat object | a dict |
JSON values must be strings, numbers or booleans, so a nested array or object is an error, and so is a CSV grid. CSV cells that look like numbers become numbers, quoted cells keep their whitespace, and blank lines are skipped.
Data files are separate from the module form, import "./lib.knip";, which has no parentheses.
Data can be loaded in an imported module too; the path is then relative to that module.
Slices
Section titled “Slices”value[start:stop:step] works on a str or a list. Like everything else here it is 1-based
and stop-inclusive, matching range():
private s: str = "abcdef";s[2:4]; # "bcd"s[:2]; # "ab"s[5:]; # "ef"s[1:5:2]; # "ace"s[::-1]; # "fedcba"
private part = scores[2:3]; # a new listfor (x, scores[2:]) { ... } # a slice can be walkedscores = scores[2:]; # or replace the listA bound past the end stops at the end. A step of 0 is an error. If the step is computed rather than a literal, both bounds are required, because an omitted bound depends on its sign. A dict cannot be sliced, and neither can a list of structs.
A string slice with literal bounds covering at most 8 letters, such as s[2:4], becomes letter of
and join blocks. Any other slice calls a procedure, added once to the project, which runs
without a screen refresh.
A slice is the only whole-list value so far: a = b;, private c = a; and a + b stop the build.
Strings behave like sequences
Section titled “Strings behave like sequences”private c: str = greeting[1]; # 1-based, operator_letter_ofprivate n: num = len(greeting);private has: bool = greeting.contains("at");
for (letter, greeting) { ... }See Slices for greeting[2:4].
Generic helpers
Section titled “Generic helpers”private paired: (list<str>, list<num>) = zip(names, powers);private tagged: (list<num>, list<str>) = enumerate(names);The types infer correctly, as T binds from the arguments. Used as a value like this, all
three of zip, enumerate and range type-check and then fail the build.
Use all three directly in a for header instead, where they fold into the loop counter and
never build anything:
for ((name, power), zip(names, powers)) { report(name, power);}See Control flow.