Check if 'banana' is in a list of approved fruits.

← Operators and Expressions · Ref: Q1101

Collection membership:

  found <- "banana" is in fruits

Reverse of 'contains' (which checks substrings). Use 'not in' for negation. Returns Boolean. See Q1099 for contains (substring), Q1102 for is in with ranges.

Example

defines module qa.operators.isinlist

  defines program

    IsInListDemo()
      stdout <- Stdout()

      fruits <- List() of String
      fruits += "apple"
      fruits += "banana"
      fruits += "cherry"

      //Check membership
      found <- "banana" is in fruits
      stdout.println(`banana in list: ${found}`)

      //Check absence
      missing <- "grape" is in fruits
      stdout.println(`grape in list: ${missing}`)
Other ways to ask this
  • I have a list of allowed values and need to test membership
  • In Python I'd use 'if item in list'. Write the EK9 equivalent
  • Given a list of strings, check whether a specific value is present
  • Test collection membership using the 'is in' expression

Coming from another language?

Python: item in list. Java: list.contains(item). Rust: list.contains(&item). Go: manual loop. EK9: item is in list — keyword expression.

Keywords: collection, check, is in, membership, contains, list