1
$\begingroup$

Consider the following list:

{{"id" -> "8792584", "name" -> "Maybank Championship Malaysia", {"marketCount" -> 22, "competitionRegion" -> "GBR"}}, {"id" -> "376041", "name" -> "Indian I League", {"marketCount" -> 20, "competitionRegion" -> "IND"}}}

I want to transform this list to the following format:

{{"id" -> "8792584", "name" -> "Maybank Championship Malaysia", "marketCount" -> 22, "competitionRegion" -> "GBR"}, {"id" -> "376041", "name" -> "Indian I League", "marketCount" -> 20, "competitionRegion" -> "IND"}}

Knowing the power of Mma, my intuition says there must be a very compact way to do this, but I have difficulty in achieving this in a clean, short way. Please help/

$\endgroup$
  • $\begingroup$ Have you tried Flatten[]? $\endgroup$ – J. M.'s discontentment Feb 19 '16 at 16:53
  • $\begingroup$ LOL - Of course. Sadly, does not do the job. $\endgroup$ – nilo de roock Feb 19 '16 at 16:55
  • $\begingroup$ Flatten[list, 1] doesn't work here, then? $\endgroup$ – J. M.'s discontentment Feb 19 '16 at 16:57
  • 2
    $\begingroup$ Answer below works, with Flatten ... I am confused. $\endgroup$ – nilo de roock Feb 19 '16 at 16:58
  • $\begingroup$ Flatten[list,1] did work ( in the case I posted in the question anyway ). $\endgroup$ – nilo de roock Feb 19 '16 at 17:08
2
$\begingroup$

Bascially you want every element of a List to be another List of depth 1. Here is an easy way to do that:

In:= rules={{"id" -> "8792584", "name" -> "Maybank Championship Malaysia", {"marketCount" -> 22, "competitionRegion" -> "GBR"}}, {"id" -> "376041", "name" -> "Indian I League", {"marketCount" -> 20, "competitionRegion" -> "IND"}}};
In:= Flatten/@rules
out= {{"id" -> "8792584", "name" -> "Maybank Championship Malaysia", "marketCount" -> 22, "competitionRegion" -> "GBR"}, {"id" -> "376041", "name" -> "Indian I League", "marketCount" -> 20, "competitionRegion" -> "IND"}}
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ That's what I wanted. $\endgroup$ – nilo de roock Feb 19 '16 at 16:58
  • 1
    $\begingroup$ {{a, {b, b}}, {a, {b, b, b}, {c, c}}} This type of structure should become { {a, b, b}, {a,b,b,b,c,c} }. Will edit the question when I get new real data. $\endgroup$ – nilo de roock Feb 19 '16 at 17:14

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.