From Kx Wiki
Jump to: navigation, search

Prerequisites: The Basics, Datatypes.



We have seen simple lists already. Can you remember how to create a list of numbers/symbols/characters? How would you add or join two lists?

q)a:1 2 3			// defining some lists
q)b:7 8 9

q)a+b				// add
8 10 12
q)c:a,b			        // join
1 2 3 7 8 9

Here are some more operators/functions which you might find useful. See if you can work out what they’re doing. Have a look at the reference page for more information.

q)first symlist
q)-2#c                          // take
8 9
1 2 3 1 2
q)1_c                           // drop/cut 
2 3 7 8 9

Single-element lists

At this point, we should discuss single-element lists. You may have noticed that using the take operator with 1 on the left (1#) gives a strange-looking result. That’s what a single-element list looks like.

q)3#c				// 3-element list
1 2 3
q)2#c				// 2-element list
1 2
q)1#c				// 1-element list
q)type 1#c
q)type first 1#c		// "first" gets an atom
q)enlist 5			// 1-element list from atom
q),5                            // but not this way!

As you can see in the example, enlist is used to create a list with one element. (The comma in the display distinguishes a 1-element list from an atom.)


What if I want to specify exactly which elements of the list to return? This is called indexing into the list, and in q the indices run from 0 to n-1 for an n-item list.

q)charlist[0]           // getting the first element of c
q)charlist[5 1 2 2]     // indexing with brackets
q)charlist 0 1 7 9      // indexing without brackets (juxtaposition)

The square brackets in the syntax are optional.

Search and comparison

We often need to search through a list for a particular value, or return all of the values that satisfy a condition. ? is the search/lookup operator, and returns the first index (only) whose value matches the input.

q)p:12 8 10 1 9 11 5 6 1 5 4 13 9 2 7 0 17 14 9 18
q)p?9		// search for 9 in p

Comparisons such as = return a boolean list. There are three 1s in the resulting list above, corresponding to the elements in c which have a value of 9. To get the indices, we can use where.

q)where p=9
4 12 18
q)p where p>10
12 11 13 17 14 18

Mixed lists

We can also have a list where the elements of the list are of different datatypes, or even lists themselves.

q)mixedlist:("hey";1;`sym;1 2 3)
1 2 3
q)type mixedlist

As you can see, the syntax for creating a mixed list separates each element with a semicolon, and surrounds the list in parentheses. q displays each element on a separate line.

Every mixed list has type 0, but this doesn’t give very much information about the list. Use each to apply a function to every element of a list.

q)type each mixedlist
10 -7 -11 7h
q)count each mixedlist
3 1 1 3

When indexing into a mixed list, we can use the same notation as before with one addition: in square-bracket notation, a semicolon “;” allows you to index at depth:

q)mixedlist 2
q)mixedlist[0 3;1]


1. Create a string of your own name. Find the last letter of that string.

2. Copy the following list into your q process.


3. Create your own mixed list and check that its type is 0h.

4. Type out the following

mylist:("hello";"world";1 2 3 4 5;101b)

Hint: your output should look like this:



Personal tools