2
$\begingroup$

I have a list, x, where each member is itself a list of 2 coordinate points. Sometimes a member is a list of only 1 point, sometimes it is a list of more points. Note that the last member below is a list of 2 points:

x = {{{2, 1}}, {{3, 1}}, {{2, 2}}, {{5, 1}}, {{2, 1}, {3, 1}}}

For each member of the list, I want a list of the second coordinate, so that my desired output is this:

{{1},{1},{2},{1},{1,1}}

Normally, I would use Part. In fact, Part works if I apply it to each member individually:

In:= {{2,1}}[[All,2]]
Out= {1}

In:= {{2,1},{3,1}}[[All,2]]
Out= {1,1}

However I don't know how to map Part to each member of the list. It looks like I can only use "All" in the bracket input of Part so I wouldn't be able to use it in a Map input. How do I map [[All,2]] to each member of x?

Thanks

$\endgroup$
1
  • 8
    $\begingroup$ Just x[[All, All, 2]] $\endgroup$
    – Carl Woll
    Commented Jun 16, 2022 at 22:12

2 Answers 2

1
$\begingroup$

Carl Woll provided the answer in comments:

x[[All, All, 2]]

Thank you!

$\endgroup$
2
  • 1
    $\begingroup$ This is not an answer and must be deleted. $\endgroup$ Commented Jun 16, 2022 at 23:52
  • $\begingroup$ I have edited the answer to show the pertinent information from the comments. If you still feel editing was never an option, and it still "must be deleted" then so be it. $\endgroup$ Commented Jun 17, 2022 at 0:20
-1
$\begingroup$

Using Map:

Map[Last /@ # &, x]

{{1}, {1}, {2}, {1}, {1, 1}}

$\endgroup$
1
  • 3
    $\begingroup$ Map has a third argument that lets you specify the level at which the function shall be applied: in this case, Map[Last, x, {2}] $\endgroup$
    – Roman
    Commented Jun 17, 2022 at 6:13

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