# Ways to access lists inside lists

I have gone through some reference material but I am not getting really good links that will help me grow understanding of pure functions,patterns and list manipulations combined together. Most of the examples referred in documentation are for single list(without sublists).

For example, say from a list I wanted to pick first element of every sublist, say,fi = FactorInteger[12]so I used

Table[fi[[i, 1]], {i, 1, Length[fi]}]


I try to do everything with Table. Though it should be achievable via other simple ways too. Can someone please put some examples where one can get to learn how to access data from lists of sublists and how to decompose it. This might sound pretty trivial to lot of people but I couldn't find books and links either that can give insight into combining these Mathematica features together.

-
Did you have a look at guide/ListManipulation? For the given example fi[[All, 1]], First /@ fi and #[[1]] & /@ fi would work. – Sjoerd C. de Vries Jul 11 '13 at 19:20
yes I have seen – Rorschach Jul 11 '13 at 19:21
For what it's worth, in your particular example, the Table expression can be replaced with # & @@@ fi. Is that the sort of thing you're interested in? – m_goldberg Jul 11 '13 at 19:22
yes..these kind of examples to practice with – Rorschach Jul 11 '13 at 19:22
I'm sorry to say, but everything you're asking for is easily found in the built-in documentation. I'm voting to close this. – Sjoerd C. de Vries Jul 11 '13 at 19:31

## 1 Answer

I'm going to answer this as I think it is helpful to gather multiple methods in one place, and such a list is not, as far as I know, easily found in the documentation.

a = FactorInteger[269325];  (* sample data *)

a[[All, 1]]
First[a\[Transpose]]
a.{1, 0}
First /@ a
# & @@@ a
#[[1]] & /@ a


All lines output: {3, 5, 7, 19}.

a[[All, 1]] is I believe the fastest general method, and should usually be your first choice.

First[a\[Transpose]] (this looks better in a Notebook) is a fast method for rectangular data.

a.{1, 0} shows a numeric method using Dot that is applicable to arrays of known dimensions, such as the output of FactorInteger.

First /@ a is probably the most explicit and easiest to read.

# & @@@ a illustrates the use of pure functions and Apply at level one.

Be aware that the latter methods are often slower because they will unpack.

Here are timings for these methods on packed data:

SetAttributes[timeAvg, HoldFirst]
timeAvg[func_] := Do[If[# > 0.3, Return[#/5^i]] & @@ Timing @ Do[func, {5^i}], {i, 0, 15}]

a = RandomInteger[1*^9, {500000, 2}];

a[[All, 1]]          // timeAvg
First[a\[Transpose]] // timeAvg
a.{1, 0}             // timeAvg
First /@ a           // timeAvg
# & @@@ a            // timeAvg
#[[1]] & /@ a        // timeAvg


0.00512

0.0012976

0.011984

0.04304

0.2122

0.04492

And unpackable data:

a = RandomChoice[{Pi, "x", 1}, {500000, 2}];

a[[All, 1]]          // timeAvg
First[a\[Transpose]] // timeAvg
a.{1, 0}             // timeAvg
First /@ a           // timeAvg
# & @@@ a            // timeAvg
#[[1]] & /@ a        // timeAvg


0.01684

0.02308

0.2122

0.078

0.0968

0.1592

-
If you're listing them all, how about Part[a, All, 1] – bill s Jul 11 '13 at 20:01
@bills That's just the long form of a[[All, 1]], and I didn't include the long form of any of these. Do you think these should be included? – Mr.Wizard Jul 11 '13 at 20:04
Wizard - I kind of like the idea of "all the ways to do it", but it's your call... – bill s Jul 11 '13 at 20:05
Here's one you dont have: First@Transpose[a] or even more obscurely First@Thread[a] – bill s Jul 11 '13 at 20:07
@Blackbird You're welcome, and thanks, I try. – Mr.Wizard Jul 13 '13 at 7:36