Runtime & expressions

Collections

The set-like object returned by `.selected` and `.values`. Every method, with examples. Most routing logic in a Fieldbase survey lives here.

What a collection is

A collection is a set-like runtime object holding zero or more codes (or values, depending on context). It’s what you get back from:

  • Q.selected on single / multi / rank / gridSingle / gridMulti rows
  • Q.values on rank, numberList, openList, grid rows / cols
  • Q.all and Q.allLabels (the visible domain)
  • Q.selectedLabels (parallel to .selected but with labels)

Collections compose with each other: pass one to a method that takes codes, and it works. They also compose with plain Python lists and set(...) literals.

Membership tests

.any(value | list) — at least one match

Q.selected.any(2)            # True if code 2 is selected
Q.selected.any([2, 3, 4])    # True if any of 2/3/4 is selected

The most common collection method. Useful for “did they pick at least one of these?”.

.every(list) — all match

Q.selected.every([1, 2])     # True only if BOTH 1 AND 2 are selected

value in collection — Python operator

2 in Q.selected              # True if code 2 is selected

Supported for symmetry with Python lists; equivalent to .any(2). The canonical form for membership checks is .any(...) — prefer it when reading or writing in expressions.

.contains(value) — legacy alias

Q.selected.contains(5)       # legacy — prefer .any(5)

contains is a non-canonical alias kept for readability. Reach for .any(value) in new code.

Size

Q.selected.size              # how many selected

Useful as a guard:

if Q.selected.size == 0:
    # they didn't answer — defensive

Set operations

These return new collections, leaving the originals untouched.

.intersect(other) — common codes

Q.selected.intersect([1, 2, 3])
# returns the codes that are both in Q.selected AND in [1, 2, 3]

Often used as a truthy check — non-empty intersection means “at least one match”:

if Q.selected.intersect([1, 2, 3]):
    # at least one of 1/2/3 was selected

.union(other) — combined codes

QA.selected.union(QB.selected)
# all codes selected in either QA or QB, deduplicated

.minus(other) — codes not in other

Q.selected.minus([5])
# Q.selected without code 5

other can be a list, a set, or another collection.

Element access

.at(index) — code at position

Q.selected.at(0)    # first code (collections are 0-indexed)
Q.selected.at(2)    # third code

For rank, the index is the rank position — .at(0) is the top-ranked item.

Iteration

Collections iterate like Python sequences:

for code in Q.selected:
    print("Selected:", code)

# enumerate works too
for i, code in enumerate(Q.selected):
    print(i, code)

For pure membership checks, prefer .any(...) over iterating — it’s the canonical surface and is faster to read.

Mutating collections

.selected collections on multi and gridMulti.row(N) support in-place mutation through the question / row receiver:

QFeatures.add(7)             # add code 7 to the multi selection
QFeatures.remove(7)          # remove code 7
QGrid.row(2).add(3)          # add code 3 to row 2's gridMulti selection
QGrid.row(2).remove(3)

These are convenience helpers for incremental writes; for a full replacement, use .set([...]) (see Question proxies).

Composition with set(...)

The runtime exposes set([...]) for constructing ad-hoc sets:

allowed = set([1, 2, 3])
if Q.selected.intersect(allowed).size > 0:
    # at least one allowed code was picked

# set literal can be on either side
set([1, 3]).intersect(Q.selected)

Common patterns

Terminate if “None of these” was picked

- script: |
    if QBrand.selected.any(99):
        endSurvey("terminate")

Require at least one of a set

- script: |
    if not QFeatures.selected.intersect([1, 2, 3]):
        endSurvey("terminate")

Mask follow-up to overlap

- qid: QFav
    type: single
    options.from: QBrand.selected.intersect(allowedList)

Combine answers from two questions

- script: |
    everything = QBrandPast.selected.union(QBrandNow.selected)
    print("Brand exposure:", everything.size)

Subtract the “None” code before piping

- qid: QFav
    type: single
    options.from: QBrand.selected.minus([99])

See also