Tutorials

Lists

List is also a fundamental data type for most of the other data structures of KDB. The datatypes like Dictionary, Table, Enumerations etc. are made of Lists. And that is the reason that the type value for these datatypes is always positive e.g. a a Table has type value of 98, a Dictionary has type value of 99 and an Enumeration has that of 20 etc.

A List can be a uniform list of elements of same datatype. The type value for such list is the positive type value for the datatype.

A general list could be a list of atoms of mixed datatypes or a list of lists containing either mixed or uniform atoms. This is represented by the items(atoms or lists) separated by “;”.

A uniform list of number data types could be represented by the atoms separated by space followed by the type symbol e.g. i, j, f etc.

q)type 25 47 12 43 234
7h
q)type 23 54 65 34 12i
6h
q)type 23.0 54 65 34 12
9h

List of binary atoms is represented by the items without any space followed by char b

q)type 01001b
1h

A List of uniform type is stored contiguously while a general list has  pointers stored contiguously.

There are a number of functions relevant to List datatype. These are mostly optimized for efficiency and speed.

Some of the frequently used functions are count, first, last, sum, min, max, avg, sums, maxs, mins, mavg, prd, prds, distinct etc.

List Functions

count

Returns count of the items in the List

q)count 23 54 65 34 12i
5

The return type of count is long. In case of general lists, count returns the count of items from the top most layer: –

q)count(23 34;23 54 65 34 12i;32.0 84.2 75;21 31)
4

The keyword/iterator each is used to traverse into each item of the list: –
q)count each(23 34;23 54 65 34 12i;32.0 84.2 75;21 31)
2 5 3 2

This returns a list of counts.