Skip to content

# Set Attribute

x#y    #[x;y]

Where y is a list or dictionary and atom x is

  • an item from the list `s`u`p`g, returns y with the corresponding attribute set
  • the null symbol `, returns y with all attributes removed


`s#2 2 3   sorted    items in ascending order  list, dict, table
`u#2 4 5   unique    each item unique          list
`p#2 2 1   parted    common values adjacent    simple list
`g#2 1 2   grouped   make a hash table         list

Setting or unsetting an attribute other than sorted causes a copy of the object to be made.

s, u and g are preserved on append in memory, if possible. Only s is preserved on append to disk.

q)t:([1 2 4]y:7 8 9);`s#t;attr each (t;key t)

Applying p attribute is faster and uses less memory since 4.1t 2023.01.20.


The sorted attribute can be set on a simple or mixed list, a dictionary, table, or keyed table.

q)`s#1 2 3
`s#1 2 3
q)`#`s#1 2 3
1 2 3

Setting the sorted attribute on an unsorted list signals an error.

q)`s#3 2 1
  [0]  `s#3 2 1

Setting/unsetting the sorted attribute on a list which is already sorted will not cause a copy to be made, and hence will affect the original list in-place.

Setting the sorted attribute on a table sets the parted attribute on the first column.

q)meta `s#([] ti:00:00:00 00:00:01 00:00:03; v:98 98 100.)
c | t f a
--| -----
ti| v   p
v | f    

Setting the sorted attribute on a dictionary or table, where the key is already in sorted order, in order to obtain a step-function, sets the sorted attribute for the key but copies the outer object.


The unique attribute can be set on simple and mixed lists where all items are distinct.

Grouped and parted

Attributes parted and grouped are useful for simple lists (where the datatype has an integral underlying value) in memory with a lot of repetition.

The parted attribute asserts all common values in the list are adjacent. The grouped attribute causes kdb+ to create and maintain an index (hash table).

If the data can be sorted such that p can be set, it effects better speedups than grouped, both on disk and in memory.

The grouped attribute implies an entry’s data may be dispersed – and possibly slow to retrieve from disk.

The parted attribute is removed by any operation on the list.

q)`p#2 2 2 1 1 4 4 4 4 3 3
`p#2 2 2 1 1 4 4 4 4 3 3
q)2,`p#2 2 2 1 1 4 4 4 4 3 3
2 2 2 2 1 1 4 4 4 4 3 3
The grouped attribute is presently unsuitable for cycling through a small window of a domain, due to the retention of keys backing the attribute.
q)0N!.Q.w[]`used; v:`g#`#v; .Q.w[]`used


s-fail   not sorted ascending
type     tried to set u, p or g on wrong type
u-fail   not unique or not parted


Some q functions use attributes to work faster:

Setting attributes consumes resources and is likely to improve performance only on lists with more than a million items. Test!

Applying an attribute to compressed data on disk decompresses it.

Q for Mortals §8.8 Attributes