# Creating new lists

We have the following list

data = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,-1,14}}


If I want to create a new list containing the first and the fourth elements of the first list we use

data2 = data[[All, {1, 4}]];


Now, what if I want the first and the Log10() of the fourth element? Log10(-1) does not exist so in the new list there should be only two sublists.

Any ideas?

• This seems like an extremely localized problem, but would Cases[data[[All, {1, 4}]], {_?Positive, _?Positive}] help you along? Oct 20, 2015 at 16:33
• Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; y > 0] Oct 20, 2015 at 16:37
• @belisariusisforth Nice work! Please write a short answer so as to approve it. Oct 20, 2015 at 16:41

data = {{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,-1,14}}
Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; y > 0]
(* {{1, Log[4]/Log[10]}, {6, Log[9]/Log[10]}} *)


edit

Or if you want it working for expressions too:

data = {{1, 2, 3, 4, 5}, {6, 7, 8, -1, 10}, {11, 12, 13, a, 14}};
Cases[data[[All, {1, 4}]], {x_, y_} :> {x, Log[10, y]} /; Positive[y] =!= False]
(* {{1, Log[4]/Log[10]}, {11, Log[a]/Log[10]}} *)


Here's my take:

Cases[data, {x_, Repeated[_, {2}], y_?Positive, __} :> {x, Log10[y]}]


Using belisarius/J.M. but with the new operator form of Cases

Cases[{x_, _, _, y_?Positive, _} :> {x, Log10[y]} ] @ data