Skip to content

in

Whether x is an item of y

x in y    in[x;y]

Where y is

  • an atom or vector of the same type as x, returns whether atoms of x are items of y
  • a list, returns as a boolean atom whether x is an item of y

Where y is an atom or vector, comparison is left-atomic.

q)"x" in "a"                                    / atom in atom
0b
q)"x" in "acdexyz"                              / atom in vector
1b
q)"wx" in "acdexyz"                             / vector in vector
01b
q)("abc";("def";"ghi");"jkl")in "bed"           / list in vector
010b
(110b;000b)
000b

Where y is a list, there is no iteration through x.

q)"wx" in ("acdexyz";"abcd";"wx")               / vector in list
1b
q)("ab";"cd") in (("ab";"cd");0 1 2)            / list in list
1b
q)any ("ab";"cd") ~/: (("ab";"cd");0 1 2)
1b

Further examples:

q)1 3 7 6 4 in 5 4 1 6        / which of x are in y
10011b
q)1 2 in (1 2;9)              / 1 2 is an item of y
1b
q)1 2 in ((1 2;3 4);9)        / 1 2 is not an item of y
0b
q)(1 2;3 4) in ((1 2;3 4);9)  / x is an item of y
1b

in uses Find to search for x in y.

in is a multithreaded primitive.

Queries

in is often used with select.

q)p:([p:`p1`p2`p3`p4`p5]name:`nut`bolt`screw`screw`cam;city:`london`paris`rome`london`paris)
q)select from p where city in `paris`rome
p | name  city
--| -----------
p2| bolt  paris
p3| screw rome
p5| cam   paris

Mixed argument types

Optimized support for an atom or one-item list y allows a wider mix of input types.

q)1 2. in 2
01b
q)1 2. in 1#2
01b
q)1 2. in 0#2
'type
  [0]  1 2. in 0#2
            ^
q)1 2. in 2#2
'type
  [0]  1 2. in 2#2
            ^

This optimization applies only when y is an atom or a one-item list.

Don’t rely on mixed argument types. This behavior might be removed in a future release.

Mixed argument ranks

Results for mixed-rank arguments can be unexpected.

q)3 in (1 2;3)
0b
q)3 in (3;1 2)
1b

Where the first item of a general list y is an atom, in searches y for each item of x, rather than for x as a whole:

q)1 2 in (1 2;9)
1b
q)1 2 in (9;1 2)
00b

Instead, use Match:

q)any ` ~/: (1 2;`)
1b
q)any 1 2 ~/: (9;1 2)
1b

except, inter, within, union

Search