2
$\begingroup$

Suppose I have a list of the following form:

List={{{a,b,c},{d,e,f},{g,h,i}},{{j,k,l},{m,n,o},{p,q,r}}}

I have two questions:

A) How can I convert this to a list of the following form:

Newlist={{a,b,c},{d,e,f},{g,h,i},{j,k,l},{m,n,o},{p,q,r}}

B) How can I access, say, the elements a,d,g,j,m,p?

This is probably a very basic question but I do not have previous experience with Mathematica.

Thank you,

$\endgroup$
5
  • $\begingroup$ Look at Flatten. To accomplish your task you will need to flatten at the right level. To access within lists, look at Part, shortcut [[ ]]. $\endgroup$
    – bill s
    Commented Mar 15, 2021 at 21:48
  • $\begingroup$ Flatten works! thank you. However, When I use part, can I return a list of all first elements all at once? $\endgroup$ Commented Mar 15, 2021 at 21:52
  • $\begingroup$ Yes... see All, so something like: list[[All,2]] or list[[2,All]] depending on which ones you want. $\endgroup$
    – bill s
    Commented Mar 15, 2021 at 21:54
  • $\begingroup$ Suppose I want to select elements such that the last coordinate (i.e. c,f,i, etc) are less than some specified value. I can't get All to work here. I know I can use Select, but I don't seem to know the way to do this $\endgroup$ Commented Mar 15, 2021 at 22:02
  • $\begingroup$ @mathematicastudent you'll need Select or Cases for that. See also this tutorial. $\endgroup$
    – MarcoB
    Commented Mar 16, 2021 at 1:14

4 Answers 4

8
$\begingroup$

The word List is systemically reserved. (At least in version 11.3) You will see an error message if you use List as a variable name.

So, let

Lit = {{{a, b, c}, {d, e, f}, {g, h, i}}, {{j, k, l}, {m, n, o}, {p, q, r}}}

Then my answer is

A) Lit[[1]] will produce {{a, b, c}, {d, e, f}, {g, h, i}}

B) List[[1]][[2]][[2]] will produce e

[[n]] means n-th object in the list.

And.. this is my first answer in stackexchange!

$\endgroup$
8
$\begingroup$
Clear["Global`*"]

list = {{{a, b, c}, {d, e, f}, {g, h, i}}, {{j, k, l}, {m, n, o}, {p, q, r}}};

Flatten[list, 1]

(* {{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}, {m, n, o}, {p, q, r}} *)

To get {a, d, g, j, m, p} use either

First /@ Flatten[list, 1]

(* {a, d, g, j, m, p} *)

or

list[[All, All, 1]] // Flatten

(* {a, d, g, j, m, p} *)

or

Flatten[list][[1 ;; ;; 3]]

(* {a, d, g, j, m, p} *)

or

Map[First, list, {2}] // Flatten

(* {a, d, g, j, m, p} *)

EDIT: To find x values for maximum z

SeedRandom[1234];
list = RandomInteger[10, {20, 3}]

(* {{0, 6, 9}, {6, 10, 0}, {7, 0, 0}, {8, 4, 4}, {8, 5, 9}, {7, 2, 
  8}, {4, 5, 8}, {6, 1, 6}, {1, 2, 0}, {10, 3, 6}, {6, 0, 7}, {4, 3, 
  7}, {9, 6, 6}, {0, 2, 2}, {3, 8, 4}, {1, 4, 2}, {4, 6, 1}, {5, 0, 
  6}, {3, 2, 6}, {8, 3, 3}} *)

First /@ MaximalBy[list, Last]

(* {0, 8} *)
$\endgroup$
1
  • $\begingroup$ Thanks, I'm also struggling to understand how to get the x coordinate when the z coordinate is a maximum. Any idea how to do this? Thanks in advance $\endgroup$ Commented Mar 16, 2021 at 0:19
2
$\begingroup$

For your first question:

Catenate@list

{{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}, {m, n, o}, {p, q, r}}

For your second question:

(Catenate@list)[[All,1]]

or

Transpose[Catenate@list][[1]]

both give

{a, d, g, j, m, p}

$\endgroup$
1
1
$\begingroup$
list =
  {{{a, b, c}, {d, e, f}, {g, h, i}},
   {{j, k, l}, {m, n, o}, {p, q, r}}};

Using Splice (new in 12.1)

Splice /@ list

{{a, b, c}, {d, e, f}, {g, h, i}, {j, k, l}, {m, n, o}, {p, q, r}}

Using Query to get the first elements:

Query[All, All /* Splice, 1] @ list

{a, d, g, j, m, p}

$\endgroup$

Your Answer

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

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