# Function that creates a table from two lists

I have two lists shown below:

list1 = {(0,0,0,0),(0,0,0,1),(0,0,1,0),...,(1,1,1,1)} list2 = {1,5,5,5,...,5}

How can I construct a function that outputs a table similar to this:

$$\begin{array}{c|c} \text{Initial vector} & \text{Period} \\ \hline (0,0,0,0) & 1 \\ (0,0,0,1) & 5 \\ (0,1,0,1) & 5 \\ (0,1,1,1) & 5 \\ \end{array}$$

I have experimented using Table, but it just outputs a list.

EDIT:

What is wrong with my function taken from one of the answers:

printPeriodTbl[vectors_, periods_] :=
Grid[Prepend[
Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ vectors,
periods}], {"Initial Vector", "Period"}],
Dividers -> {2 -> True, 2 -> True}];


what I am passing into this function looks like:

vectors =  {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {0, 0, 1, 1}, {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 1, 1, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1}}
period = {1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}


Errors from this are:

Transpose::nmtx: The first two levels of {Pattern[((Row[#1,,])&)[listOfInitVectors],(Row[_,,])],listOfPeriods_} cannot be transposed. >>
Transpose::tperm: Permutation {Pattern[((Row[#1,,])&)[listOfInitVectors],(Row[_,,])],listOfPeriods_} is longer than the dimensions {2} of the expression. >>

• Transpose[{list1,list2}] then use TableForm and add your table headers Feb 22, 2015 at 21:42
• You have written list1 as {(0,0,0,0),(0,0,0,1),(0,0,1,0),...,(1,1,1,1)}. Since you clearly know that lists are {} the only way to interpret this is that (0,0,0,0) must be a string. In other words list1 and list 2 are both vectors (hence Transpose). Is that actually what you mean or is it a typo? Feb 22, 2015 at 22:21

list1 = {{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}};
list2 = {1, 5, 5, 5};

Grid[Prepend[Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ list1, list2}],
{"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]


or

 Grid[Join[{{"Initial Vector", "Period"}},
Transpose[{Row[{"(", Row[#, ","], ")"}] & /@ list1, list2}]] ,
Dividers -> {2 -> True, 2 -> True}]
(* same output *)


or

tbl = Table[{Row[{"(", Row[list1[[i]], ","], ")"}], list2[[i]]}, {i, 1, Length@list1}];
Grid[Prepend[tbl, {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]
(* same  output *)


Update: If your list1 is a list of strings like

 list1b = {"(0,0,0,0)", "(0,0,0,1)", "(0,0,1,0)", "(1,1,1,1)"}


then you can simply use list1b instead of Row[{"(", Row[...],")"}].

• Based on how the question was written I thought the only way to interpret list1 was {"(0,0,0,0})",...} unless he is completely unfamiliar with Mma syntax. Feb 22, 2015 at 22:19
• @MikeHoneychurch, that makes more sense; makes it easier too.
– kglr
Feb 22, 2015 at 22:42
• Could you look at my edit please. I am confused on the issue Feb 22, 2015 at 22:42
• @connor, if your list1 is a list of strings, e.g., {"(0,0,0,0)", "(0,0,0,1)", "(0,0,1,0)", "(1,1,1,1)"} then you don't need the Row[....] piece; you can just use list1 instead of Row[{"(", Row[#, ","], ")"}] & /@ list1.
– kglr
Feb 22, 2015 at 22:49
• I actually turned them into lists, so it is currently a list of lists like {{0,0,0,1},{0,0,0,1}} Feb 22, 2015 at 22:51
Grid[Prepend[Transpose@{list1, list2}, {"Initial Vector", "Period"}], Dividers -> {2 -> True, 2 -> True}]