# How can I append associations with uneven elements? [duplicate]

My dataset is pretty simple so far:

Questions = Dataset[{
<|"Statement" -> "What is the capital of Switzerland?", "Answer" -> "Bern",
"Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>,
<|"Statement" -> "Who is the king of the jungle?", "Answer" -> "Lion",
"Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>,
<|"Statement" -> "2+2?" , "Answer" -> "4", "Incorrect Answers" -> {"15", "13", "1e15"}|>}]


If I try the following, everything goes right:

NewQuestion1 = <|"Statement" -> "To be...", "Answer" -> "or not to be",
"Incorrect Answers" -> {"or to eat", "continued", "dummy"}|>;
Append[Questions, NewQuestion1]


Nevertheless, if I delete the dummy answer of the NewQuestion, it will not work:

NewQuestion2 = <|"Statement" -> "To be...", "Answer" -> "or not to be",
"Incorrect Answers" -> {"or to eat", "continued"}|>;
Append[Questions, NewQuestion2]


This issue seems weird since you can declare a data set with uneven lists from the beginning:

Questions = Dataset[{
<|"Statement" -> "What is the capital of Switzerland?",
"Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>,
<|"Statement" -> "Who is the king of the jungle?",
"Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>,
<|"Statement" -> "2+2?" , "Answer" -> "4",
"Incorrect Answers" -> {"15", "13", "1e15"}|>,
<|"Statement" -> "To be...", "Answer" -> "or not to be",
"Incorrect Answers" -> {"or to eat", "continued"}|>
}]


Any suggestions on how to append NewQuestion2 and/or explanations of why this error is happening?

-

## marked as duplicate by RunnyKine, Mr.Wizard♦Jul 28 at 18:26

I read that question before posting mine and find it different since m_goldberg is trying to append an element of a different class (I must say that I found that quite interesting and certainly something for Mathematica to improve). Here the thing is that I want to append another list. –  Carlos Chida Jul 28 at 18:09
Insert[Questions, NewQuestion2, -1]. All credits to @m_goldberg in the linked Q&A. –  seismatica Jul 28 at 18:15
Super! Not as "elegant" as I am used to in Mathematica, but works perfectly. Thank you very much. –  Carlos Chida Jul 28 at 18:19
I voted for this being a duplicate. (Which, as a moderator, takes immediate effect.) The underlying problem appears to be the same, and the solutions are the same. Since your problem is resolved I don't see the need to keep this open longer. –  Mr.Wizard Jul 28 at 18:28
I enquire against since even though the solution is the same, the fact that I explained in the first comment here made it hard to get at the first sight that it could work. Moreover, we talking about undocumented functions and extending the its use. –  Carlos Chida Jul 28 at 18:38

Well, here is a workaround:

First, we load the package TypeSystem:

Needs["TypeSystem"]


Now we define the Dataset to expect any type:

Questions =
Dataset[{<|"Statement" -> "What is the capital of Switzerland?",
"Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>, <|
"Statement" -> "Who is the king of the jungle?",
"Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>, <|
"Statement" -> "2+2?", "Answer" -> "4",
"Incorrect Answers" -> {"15", "13", "1e15"}|>},


Note the undocumented second argument of Dataset

We can now append with no problems:

NewQuestion2 = <|"Statement" -> "To be...", "Answer" -> "or not to be",
"Incorrect Answers" -> {"or to eat", "continued"}|>;


Then

Append[Questions, NewQuestion2]
`

-
Amazing, thanks. Do you also know why this happens? And do you know where does this second argument work? For instance if I used Join to add more columns, could I also specify there that they are AnyType? –  Carlos Chida Jul 28 at 18:33
@CarlosChida, I don't know for sure why it happens but think it's a bug. The second argument is undocumented and I don't know the details of its functionality. –  RunnyKine Jul 28 at 18:37