Rhys Martin
Rhys Martin

Reputation: 191

How to create a list of length x with identical elements?

I would like to create a list in q/kdb of variable length x which contains the same element e repeated. For example:

x:4;
e:`this;
expected_result:`this`this`this`this

Upvotes: 2

Views: 1674

Answers (4)

terrylynch
terrylynch

Reputation: 13657

As mentioned by all, # is the best solution in the singular case. If you wanted to duplicate multiple items into a larger single list, then where can achieve this nicely

q)`this`that where 4 2
`this`this`this`this`that`that

Upvotes: 7

Rahul
Rahul

Reputation: 3969

Use '#'(take) function:

 q) x:4
 q) e:`this
 q) x#e

Upvotes: 2

Cmccarthy1
Cmccarthy1

Reputation: 388

You can do this using # https://code.kx.com/v2/ref/take/

q)n:4
q)vals:`this
q)n#vals
`this`this`this`this

Upvotes: 2

jomahony
jomahony

Reputation: 1692

Take is what you're looking for: https://code.kx.com/v2/ref/take/

q)x:4
q)e:`this
q)x#e
`this`this`this`this

Upvotes: 5

Related Questions