The list is an ordered seqeuence of items and the fundamental data container and building block of kdb+. Once you master creating, updating and manipulating lists, you've mastered kdb+.

Creating Lists

The full notation for defining lists surrounds the items with parentheses and has a semi-colon between each list item. Each item placed in the list can be the same type or a different type.

Type of Lists

Notice the type nums for lists of all the same type are similar to what we saw for atoms ie taken from the datatype table, but are now positive. (I like to remember this by thinking that the preferred data type in the q vector language is a large list of data.) While for a mixed list that contains different types, the type number returned is 0.

Create Simple Lists

Lists that contain all the same type of data are called simple lists or homogenous lists. Defining simple lists is common so there is a short notation for defining them. Normally this is no parentheses, separating each item with a space and placing the type letter at the end. For example:

Some types use a slightly different shorthand notation.

Create Empty or Single Item Lists

Common problems to be careful of are defining single item lists and accidentally mixing types. Surrounding a single item with parentheses does not make it a list. To make a single item into a list we must use either enlist or (),x.

Common List Functions

Common operations related to lists are shown below.

  • til n - generates the sequence of numbers from 0 to n-1 inclusive.
  • a?b - where a and b are both atoms, chooses a random numbers between 0 and b-1 inclusive.
  • count - Returns the number of items in a list

Indexing and Assignment

Lists are ordered left to right and indexing begins at 0. A single integer references a single index alternatively supplying a list of indices retrieves the relevant list of values. Referencing a non-existant index returns a null value.

For assignment the colon operator is used. Similar to casting and other list functions we can assign multiple indices in one opertaion. When assigning a new value to simple lists it must be the same type otherwise an error will be thrown.

Nested Lists

In kdb+ we can have lists of lists, this is in fact quite common. These nested lists can either be defined in one statement using the longhand parentheses notation or in steps.

Once we have a nested list we can index into the list similar to before. Though in this case referring to one index will retrieve a whole list of items.

Indexing at Depth

Rather than indexing at the topmost level, you may want to index deeper into one of the items. (In other languages this is reffered to as two-dimensional arrays.) To index-at-depth we can either use a set of square brackets for each level or separate the indices using semi-colons:

Eliding List Indices

Now that we know how to index-at-depth, what happens if we omit one level, yet specify the deeper one.

More List Functions

Common operations to perform on lists include:

  • Find l?k - where l and k are both lists, this will return the indices of l, where each item in k can be found..
  • Take A#l - where A is positive take the first A items from list l. (negative = take the last A items).
  • Drop _ - where A is positive remove the first A items from list l. (negative = remove the last A items).