# Basic manipulations with lists

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,

• Look at Flatten. To accomplish your task you will need to flatten at the right level. To access within lists, look at Part, shortcut [[ ]]. Commented Mar 15, 2021 at 21:48
• Flatten works! thank you. However, When I use part, can I return a list of all first elements all at once? Commented Mar 15, 2021 at 21:52
• Yes... see All, so something like: list[[All,2]] or list[[2,All]] depending on which ones you want. Commented Mar 15, 2021 at 21:54
• 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 Commented Mar 15, 2021 at 22:02
• @mathematicastudent you'll need Select or Cases for that. See also this tutorial. Commented Mar 16, 2021 at 1:14

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}}}


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!

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} *)

• 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 Commented Mar 16, 2021 at 0:19

Catenate@list


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

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


or

Transpose[Catenate@list][[1]]


both give

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

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}