2
$\begingroup$

I am starting to use the new Associations, so this maybe an easy question, but I haven't figured it out. I have a list of associations, like:

{<|k1->v1,k2->v2, ..., k3->v3|>, <|q1->w1,q2->w2, ..., q3->w3|>, ...}

and I would like to extract a list of data pairs of the form

{{v1,v2}, {w1,w2}, ...}

How can I do this?

$\endgroup$
3
$\begingroup$

There are pure association forms using Part with key names or positions for this.

(* Using key names *)
Values[assoc[[All, {"one", "two"}]]]

(* using positions *)
Values[assoc[[All, 1 ;; 2]]]
$\endgroup$
1
  • $\begingroup$ this doesn't seem to work if the keys in the association are arbitrary real numbers $\endgroup$
    – Michael
    May 15 '19 at 23:02
3
$\begingroup$

Building an example association:

tab = Table[{"one" -> i, "two" -> i^2, "three" -> i^3}, {i, 1, 10}];

and

assoc = Association @@@ tab;

If I understand your question right you are only interested in the first two values, so you can do the following:

vals = Values @ tab

and

Take[#, 2] & /@ vals

But if you want to process further on, it might be a good idea to store the information in a dataset ds = Dataset[assoc] then you can simply extract the columns you want.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.