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 (9;(1 2;3 4))        / no item of x is in y
00b
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

Queries

in is often used with select.

q)\l sp.q
q)select from p where city in `paris`rome
p | name  color weight city
--| ------------------------
p2| bolt  green 17     paris
p3| screw blue  17     rome
p5| cam   blue  12     paris

Mixed argument types

Optimized support for atom or 1-list y allows a wider input type mix.

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
            ^

There is no plan to extend that to vectors of any length, and it might be removed in a future release.

We strongly recommend avoiding relying on this.

Mixed argument ranks

Results for mixed-rank arguments are not intuitive

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

Instead use Match:

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

except, inter, union
Search