# Replacing elements of a nested list by some value

I have a list of list

A= {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177,
1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067, 0.744916,
0.720972, 0.674833, 1.29773, 1.29514}, {0.620161, 0.320312, 0.94842,
1.11844, 1.12045, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388,
0.750067, 0.744916, 0.720972, 0.674833, 1.29383,
1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539,
1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916, 0.720972,
0.674833, 1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844,
1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916,
0.720972, 0.674833, 1.29383, 1.29514}, .... }}.


The number of lists inside is not known. I want to replace all the elements of the first list by 20, all the elements of the second list by 20.1 and so on for all the lists.

I tried ReplaceList but with no success, can anyone help me with this?

• You can start with list[[1,;;]]=20. It should be easy to generalize this in the way you need it. Oct 11 '14 at 7:47
• Yes, I know how to do list by list, but I don't know how to generalize or do it automatically for all the lists. Even the resources on Wolfram don't say most of the stuff that are discussed here. Oct 11 '14 at 8:10

ClearAll[miF, rPF, tBF];
miF = MapIndexed[20 + .1 (#2[[1]] - 1) &, #, {2}] &;


or

rPF = ReplacePart[#, {i_, _} :> (19.9 + 0.1 i)] &;


or

tbF = Block[{j = 0}, Table[ 0 i + 20 + 0.1 j++, {i, #}]] &;


Example:

A = {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177,
1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067,
0.744916, 0.720972, 0.674833, 1.29773, 1.29514}, {0.620161,
0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177, 1.13142,
1.15048, 0.721388, 0.750067, 0.744916, 0.720972, 0.674833,
1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045,
1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916,
0.720972, 0.674833, 1.29383, 1.29514}, {0.620161, 0.320312,
0.94842, 1.11844, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388,
0.750067, 0.744916, 0.720972, 0.674833, 1.29383, 1.29514}};

miF@A
(* {{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.},
{20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,  20.1, 20.1, 20.1, 20.1, 20.1,
20.1, 20.1, 20.1, 20.1},
{20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2,
20.2, 20.2, 20.2, 20.2},
{20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3,
20.3, 20.3, 20.3}} *)

miF @ A == rPF @ A == tbF @ A
(* True  *)

• To a person who has never programmed before, this seems like magic. Thank you. Oct 11 '14 at 8:08
• @Nilanjan, my pleasure. Welcome to Mathematica.SE.
– kglr
Oct 11 '14 at 8:22
• This is a rather inefficient method as you apply the function to every element rather than using a vector operation. Oct 12 '14 at 2:19

Another interesting approach:

i = 0;
Map[0 # + (20 + 0.1 i++) &, A]

{{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.,
20., 20., 20., 20., 20.}, {20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,
20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1}, {20.2, 20.2,
20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2,
20.2, 20.2, 20.2}, {20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3,
20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3}}

• The easiest approach, indeed Oct 11 '14 at 14:41

Using:

A = {{0.620161, 0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177,
1.13142, 1.15048, 1.23244, 0.721388, 0.708943, 0.750067,
0.744916, 0.720972, 0.674833, 1.29773, 1.29514}, {0.620161,
0.320312, 0.94842, 1.11844, 1.12045, 1.12539, 1.13177, 1.13142,
1.15048, 0.721388, 0.750067, 0.744916, 0.720972, 0.674833,
1.29383, 1.29514}, {0.620161, 0.320312, 0.94842, 1.11844, 1.12045,
1.12539, 1.13177, 1.13142, 1.15048, 0.721388, 0.750067, 0.744916,
0.720972, 0.674833, 1.29383, 1.29514}, {0.620161, 0.320312,
0.94842, 1.11844, 1.12539, 1.13177, 1.13142, 1.15048, 0.721388,
0.750067, 0.744916, 0.720972, 0.674833, 1.29383, 1.29514}};


A function to replace list:

rep[a_, v_] := MapThread[Table[#2, {Length@#1}] &, {a, v}]


so

rep[A, 19.9 + Range[Length[A]]/10]


yields:

{{20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20., 20.,
20., 20., 20., 20., 20.}, {20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1,
20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1, 20.1}, {20.2, 20.2,
20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2, 20.2,
20.2, 20.2, 20.2}, {20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3,
20.3, 20.3, 20.3, 20.3, 20.3, 20.3, 20.3}}

• ConstantArray would be better than Table here as the latter assumes a changing value. +1 nevertheless. Oct 12 '14 at 2:18
Table[A[[i, All]] = Table[20. + n/10, {n, 0, Length[A] - 1}][[i]], {i, 1, Length[A]}];

A // TableForm


19.9 + Range@Length@A/10 + 0 A

• Why is it oftentimes so difficult to see the simple things?
– eldo
Oct 12 '14 at 15:15

Two more:

19.9 + #1/10 + 0 #2 & @@@ Transpose@{Range@Length@A, A}


or

Transpose@{Range@Length@A, A} /. {x_, y_} :> 0 y + 19.9 + x/10