13
$\begingroup$

What are some easy ways to convert a List of Rules

{"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98}

into a List of Lists

{{Joe, 94}, {Jane, 85}, {Bob, 82}, {Bill, 83}, {Michelle, 98}}

?

$\endgroup$
1
  • $\begingroup$ Do you also want to remove the quotations in your list of lists? $\endgroup$ – QuantumDot Nov 29 '15 at 12:18
23
$\begingroup$
list = {"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98};

List @@@ list

{{"Joe", 94}, {"Jane", 85}, {"Bob", 82}, {"Bill", 83}, {"Michelle",
98}}

which is a short notation, thanks @ Alexey Popkov, for

Apply[List, list, {1}]

Or

list /. Rule :> List

Or

Extract[#, {{1}, {2}}] & /@ list
$\endgroup$
3
  • $\begingroup$ Should that Apply[List,list,2] be Apply[List,list,1]. I took your example, went to the documentation, and found: The short form @@@ is equivalent to applying at level 1. Next, I tried FullForm[list] and your suggestion list/.Rule:>List made immediate sense, but list/.Rule->List also worked. Thanks for some great answers. Still working on the last one. $\endgroup$ – David Nov 28 '15 at 18:41
  • 1
    $\begingroup$ Actually List @@@ list is equivalent to Apply[List, list, {1}] as you can immediately see from List @@@ list // Hold // InputForm. $\endgroup$ – Alexey Popkov Nov 28 '15 at 23:03
  • $\begingroup$ Thanks, I updated to correct this mistake $\endgroup$ – eldo Nov 29 '15 at 11:27
12
$\begingroup$

for something different, for any list of rules e.g.

list = {"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98}

then

list[[All, 0]] = List;list

will return what you want

$\endgroup$
1
  • $\begingroup$ Very neat, never thought of that! $\endgroup$ – sebhofer Dec 1 '15 at 9:17
4
$\begingroup$
data = {"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98};

As Eldo points out

List @@@ data

is most likely the best way to do this. But a destructing function would be fairly competitive. It also provides a direct visual representation of the operation sought, which makes it easy to come to mind.

f[k_ -> v_] := {k, v}
f /@ data

Starting with V10, there are two new ways to this.

Apply[List] /@ data

and

Association[data] // KeyValueMap[List]

The last is a little kinky though.

$\endgroup$
3
  • $\begingroup$ I got your function f utility to work like this: data/.(k_->v_)->{k,v}. And the last kinky one gives me an introduction to moving back and forth from associations to lists. Thanks. Nice answer. $\endgroup$ – David Nov 28 '15 at 19:01
  • $\begingroup$ I would imagine, that Association[data] may lead to a large overhead. That is, once an Association object is in memory, performing operations on it may be fast, but constructing the Association might be quite slow. $\endgroup$ – LLlAMnYP Nov 29 '15 at 13:28
  • $\begingroup$ +1 My application starts with an association, and I need to convert <| key -> val,...|> into {{key,val},...}. KeyValueMap is more than 50% faster than something like List@@@(Normal@assoc). $\endgroup$ – bobthechemist Mar 20 '16 at 13:29
1
$\begingroup$
l= {"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, 
   "Michelle" -> 98};
Transpose[{l[[;; , 1]], l[[;; , 2]]}]
$\endgroup$
1
$\begingroup$

{"Joe" -> 94, "Jane" -> 85, "Bob" -> 82, "Bill" -> 83, "Michelle" -> 98} //. {a___, Rule[s_ , f_], g___} :> {a, {s, f}, g}

=> {{"Joe", 94}, {"Jane", 85}, {"Bob", 82}, {"Bill", 83}, {"Michelle", 98}}

$\endgroup$

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.