# How to complete list of data with integers?

I have a data list with two columns {xi, yi}. The numbers in the left column for xi are integers. For example:

A = {{1,0.01},{7,0.064},{8,0.032},{9,0.1},{10,0.9},{23,0.76},{32,0.98},{96,0.56}}


I want to complete the list files to have a total of 99 data. Then, in the left column xi, integers must be entered. The corresponding yi will be 0.

A'={{{1,0.01},{2,0},{3,0},{4,0},{5,0},{6,0},{7,0.064},{8,0.032},{9,0.1},{10,0.9},{11,0}...{22,0},{23,0.76},{24,0}...{31,0},{32,0.98},{33,0}..,{95,0},{96,0.56},{97,0},{98,0},{99,0}}}


A' is modifications in set A. I believe the option to be used is Insert

• {10,0,9} - a typo? Commented Oct 28, 2017 at 13:28
• mistake of typing @VitaliyKaurov
– SAC
Commented Oct 28, 2017 at 15:34
• @SAC please fix the typo you have in the data in the post itself. Commented Oct 28, 2017 at 16:47

MapIndexed[{#2[[1]], #} &, SparseArray[Rule @@@ A, {99}]]


or, in versions 10.0+,

MapIndexed[{#2[[1]], #} &] @ SparseArray[Rule @@@ A, {99}]


{{1, 0.01}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0.064}, {8, 0.032},
{9, 0.1}, {10, 0.9}, {11, 0}, {12, 0}, {13, 0}, {14, 0}, {15, 0}, {16, 0},
{17, 0}, {18, 0}, {19, 0}, {20, 0}, {21, 0}, {22, 0}, {23, 0.76}, {24, 0},
{25, 0}, {26, 0}, {27, 0}, {28, 0}, {29, 0}, {30, 0}, {31, 0}, {32, 0.98},
{33, 0}, {34, 0}, {35, 0}, {36, 0}, {37, 0}, {38, 0}, {39, 0}, {40, 0},
{41, 0}, {42, 0}, {43, 0}, {44, 0}, {45, 0}, {46, 0}, {47, 0}, {48, 0},
{49, 0}, {50, 0}, {51, 0}, {52, 0}, {53, 0}, {54, 0}, {55, 0}, {56, 0},
{57, 0}, {58, 0}, {59, 0}, {60, 0}, {61, 0}, {62, 0}, {63, 0}, {64, 0},
{65, 0}, {66, 0}, {67, 0}, {68, 0}, {69, 0}, {70, 0}, {71, 0}, {72, 0},
{73, 0}, {74, 0}, {75, 0}, {76, 0}, {77, 0}, {78, 0}, {79, 0}, {80, 0},
{81, 0}, {82, 0}, {83, 0}, {84, 0}, {85, 0}, {86, 0}, {87, 0}, {88, 0},
{89, 0}, {90, 0}, {91, 0}, {92, 0}, {93, 0}, {94, 0}, {95, 0}, {96, 0.56},
{97, 0}, {98, 0}, {99, 0}}

You can first create a list with zeros and then replace selected entries.

A = {{1, 0.01}, {7, 0.064}, {8, 0.032}, {9, 0.1}, {10, 0.9},
{23, 0.76}, {32, 0.98}, {96, 0.56}};

data = Table[{i, 0}, {i, 99}];
(data[[#[[1]], 2]] = #[[2]]) & /@ A

   c = {#, 0} & /@ Range[99] ;
c [[A[[All, 1]], 2]] = A[[All, 2]];


A basic approach :

A = {{1, 0.01}, {7, 0.064}, {8, 0.032}, {9, 0.1}, {10, 0.9}, {23, 0.76}, {32, 0.98}, {96, 0.56}};
A = Join[A, Table[{i, 0}, {i, 1, 99}]];
A = Sort@DeleteDuplicates[A, First@#1 == First@#2 &]


{{1, 0.01}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0.064}, {8, 0.032}, {9, 0.1}, ... , {99, 0}}

One-line version :

Sort@DeleteDuplicates[Join[A, Table[{i, 0}, {i, 1, 99}]], First@#1 == First@#2 &]

a = {{1, 0.01}, {7, 0.064}, {8, 0.032}, {9, 0.1}, {10, 0.9}, {23, 0.76}, {32, 0.98}, {96, 0.56}};

list = Table[{i, 0}, {i, 99}];


Using SubsetMap (new in 12.0)

SubsetMap[a &, list, First /@ a]


{{1, 0.01}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, 0.064}, {8, 0.032}, {9, 0.1}, {10, 0.9}, {11, 0}, {12, 0}, {13, 0}, {14, 0}, {15, 0}, {16, 0}, {17, 0}, {18, 0}, {19, 0}, {20, 0}, {21, 0}, {22, 0}, {23, 0.76}, {24, 0}, (...), {96, 0.56}, {97, 0}, {98, 0}, {99, 0}}

a = {{2, x}, {7, y}, {10, z}};

list = Table[{i, 0}, {i, 10}];

SubsetMap[a &, list, First /@ a]


{{1, 0}, {2, x}, {3, 0}, {4, 0}, {5, 0}, {6, 0}, {7, y}, {8, 0}, {9, 0}, {10, z}}

The data

 A0=Flatten[{{1,0.01},{7,0.064},{8,0.032},{9,0.1},{10,0.9},
{23,0.76},{32,0.98},{96,0.56}},{1}];


Now, generate table with the additional entries.

 idx = DeleteCases[ A0[[All,1]] , Range[99]];
B0  = Table[{i,0}, {i,  Complement[Range[99],idx] } ];


And now simply join them

Sort@ArrayFlatten[{{A0},{B0}}]//MatrixForm

result =
KeyValueMap[List] @ Association[Thread[Range[99] -> Array[0 &, 99]], Rule @@@ A];

Partition[result /. {a_, b_Real} :> {a, Style[b, Bold]}, UpTo @ 8] // Grid


Using ReplacePart:

A = {{1, 0.01}, {7, 0.064}, {8, 0.032}, {9, 0.1}, {10, 0.9}, {23,
0.76}, {32, 0.98}, {96, 0.56}}

If[MemberQ[A, #], Style[#, Red, Bold], #] & /@
Transpose[{Range[99], ReplacePart[Table[0, 99], Rule @@@ A]}]