Testing Membership in a Collection
Membership asks whether an accepted value exists in a collection under that collection's comparison or lookup rule.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Choose the element type
In Python, "tea" in ["tea", "rice"] checks whether that string is one of the listed elements. A string on the right searches for a substring, so "tea" in "steak" is also true. Types and supported operations explain why the same operator answers distinct questions.
Check the intended label rule
When permitted status values are whole strings, a tuple, set, or a map of valid statuses expresses whole-value membership. Try "pay" against a collection containing only "paid". A canonicalization choice also determines whether differently cased valid values match.[1][2]
Choose membership at the correct level of structure
The text "paid,open" contains the substring "aid". A collection containing the two complete labels paid and open has no member named aid. To check for a whole status code, parse a delimited list into elements, or keep a set of codes from the start. If whitespace and case matter, define the exact canonicalization before insertion and lookup. See normalized labels.
Retain the identity the question depends on
A dictionary membership test answers whether a key is present, even if the associated value is zero or null. Testing its retrieved value for truth asks something else. For a table of permitted resources, an empty description may still belong to a valid key. Review dictionary lookups and use an explicit absence marker if necessary.
Design tests for one exact member, one incomplete substring, one absent key, and a present key holding a false value. If the user asked for “appears in any text field,” write that as a separate search rule. It can have different boundary and punctuation behavior from membership in a collection.