10
$\begingroup$

I have an asymmetrical nested list. Each element of the deepest list represents a person. So in this example there are three people.

data = {{4, 7, 0}, {10, 5, 10}, {{1, 4, 4}, {9, 3, 5}, {{1, 4, 9}, {10, 5, 4}}}}

Each element of the outer list has different depths. The form is: data = {data1, data2, {data3, data4, {data5, data6}}}

I want to extract the first element (or the first person) of the deepest list for each element, while keeping the same nested list format.

The desired output is {4,10,{1,9,{1,10}}}

I do not know the depth of each nested list, but want code that would look for the deepest list within each element and extract that first element.

$\endgroup$

1 Answer 1

10
$\begingroup$

Here is one possibility:

Replace[data, {a_,__} :> a, {-2}]

{4, 10, {1, 9, {1, 10}}}

$\endgroup$
1
  • 3
    $\begingroup$ Equivalently: Map[First, data, {-2}]. $\endgroup$ Nov 14, 2017 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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