Skip to content
Ferenc Bodon edited this page Jul 23, 2018 · 31 revisions

Creation

command Q Python Q example Python example comment
creating general list ( ) with separator ; [ ] with separator , l: (3; 1.0; "hello") l = [3, 1.0, "hello"]
πŸ’™ creating homogenious list whitespace is the separator li: 3 1 2 li = [3, 1, 2] Q saves you some typing
πŸ’› list of a single item keyword enlist no special treatment enlist 3 [3] Need of using enlist is confusing
list of pairs from two lists , ' zip l1 ,' l2 zip(l1, l2)
πŸ’™ list of generated ints til range and list til 3 list(range(3))
πŸ’› list of generated ints between not supported, workaround exists 3 + til 7-3 list(range(3,7))
πŸ’› list of generated ints with steps workaround exists 4 + 2 * til (10 - 4) div 2 list(range(4,10, 2))

Modification

command Q Python Q example Python example comment
append ,: append l,: 3 l.append(3)
πŸ’™ extend ,: extend l1,: l2 l1.extend(l2) Q implements polymorphism better
πŸ’› insert into middle not supported, workaround exists insert (i#l), x, i_l l.insert(i, x)
delete by index # del l: (3#l), 4_l del l[3]
delete all # clear l: 0#l l.clear()
πŸ’› pop last element not supported, workaround exists pop l.pop()
πŸ’› remove value : and except remove l: l except 3 l.remove(3)
πŸ’™ remove multiple values l1 except l2 [e for e in l1 if e not in l2]

Lookup

command Q Python Q example Python example comment
find first ? index l ? 3 l.index(3)
πŸ’™ find multiple ? not supported l ? 3 7
πŸ’™ find all occurrences = where where 3 = l [i for i, x in enumerate(l) if x == "3]
πŸ’™ position of all occurrences of elements group not supported group 1 2 1 3 3
πŸ’™ unique values distinct manually convert to set distinct l list(set(l))
πŸ’™ intersection inter l1 inter l2 list(set(l1) & set(l2)) or set(l1).intersection(l2)
πŸ’™ union union l1 union l2 list(set(l1) | set(l2)) or set(l1).union(l2)
πŸ’™ first/last element as monadic function first/last not supported first l
first/last n rows # [:] 15#l -3#l l[:15] l[-3:]
πŸ’™πŸ’™ indexing by list same as index by number workaround with for loop l[0 3 9] [l[i] for i in [0, 3, 9]]] Q is more elegant and requires less typing. Class array in Numpy also supports indexing by list

Operators

command Q Python Q example Python example comment
concatenation , + l1, l2 l1 + l2
equivalence ~ == l1 ~ l2 l1 == l2
empty list check count not 0 = count l not l
lexicographical comparison not first iasc <= not first iasc (l1; l2) l1 < l2 In Q operators < and * is reserved for elementwise comparison
list repetition raze enlist * raze 3#enlist l 3*l
πŸ’™πŸ’™ elementwise operators by design not suported
sorting asc sorted asc l sorted(l) inplace sorting in only supported in Python by function sort
πŸ’™ desc desc l sorted(l, reverse=True)
πŸ’™ overwrite null only ^ not supported (1 2) ^ 0N 4

Clone this wiki locally