I am currently trying to generate a list of permutations of length 2 of elements of a list of strings. For example:

Permutations[{"s1", "s2", "s3", "s4"}, {2}]
{{"s1", "s2"}, {"s1", "s3"}, {"s1", "s4"}, {"s2", "s1"}, {"s2", "s3"}, {"s2", "s4"}, {"s3", "s1"}, {"s3", "s2"}, {"s3", "s4"}, {"s4", "s1"}, {"s4", "s2"}, {"s4", "s3"}}

which is quite correct.

Now, my problem starts when attempting the same operation for a really big list of strings imported from a tsv file - we are talking of a bit more than 10000 strings. All sort of characters can be expected in strings.

This is the code that I am using:

t1 = Import["/home/bla/work/parsed_names.tsv"]
t2 = Table[t1[[i]][[1]], {i, 1, Length[t1]}]
Permutations[t2, {2}]

After evaluation of the last line, I get no output. If I evaluate it twice, all the symbols defined previously (t1, t2) become undefined, and Permutations finally give this output:

Permutations[t2, {2}]
Permutations::normal: Nonatomic expression expected at position 1 in Permutations[t2,{2}]. >>
Permutations[t2, {2}]

Which is the output for an undefined value of t2.

I tried to debug the problem: the import operations seems to go fine, each element of t2 is a proper string:


Tally[Table[Head[t2[[i]]], {i, 1, Length[t2]}]]
{{String, 10525}}

I tried running Trace to understand what is going on, but Mathematica still kept quiet during the first evaluation. The second evaluation again forces all the symbols to be cleared, and the output of Trace is unhelpful:

{Permutations[t2,{2}], {Message[Permutations::normal,1,Permutations[t2,{2}]],
 {MakeBoxes[Permutations::normal: Nonatomic expression expected at position 1 in Permutations[t2,{2}]. >>,StandardForm],
  RowBox[{StyleBox[RowBox[{Permutations,::,normal}],MessageName],: ,
   "Nonatomic expression expected at position \!\(1\) in \!\(Permutations[\(\(t2, \(\({2}\)\)\)\)]\). \!\(\*ButtonBox[\">>\", ButtonStyle->\"Link\", 
    ButtonFrame->None, ButtonData:>\"paclet:ref/message/General/normal\", ButtonNote -> \"Permutations::normal\"]\)"}]},Null},Permutations[t2,{2}]}

Is there something obviously wrong that I am doing? If not, how can I debug this problem further?

  • $\begingroup$ Can you upload the tsv file compressed? $\endgroup$
    – M.R.
    Aug 29, 2012 at 19:52
  • $\begingroup$ If you simply type, "t1" and hit shift + enter, what does mms show? $\endgroup$
    – DavidC
    Aug 29, 2012 at 19:58
  • $\begingroup$ @Mike: unfortunately I cannot $\endgroup$
    – paolo
    Aug 29, 2012 at 20:00
  • $\begingroup$ Look at how it scales: ListLogLogPlot@Table[{i, Length[Permutations[Range[i], {2}]]}, {i,10^Range[1, 3., .2]}] gives this. It's probably related (but I don't see exactly what is happening) $\endgroup$
    – acl
    Aug 29, 2012 at 20:01
  • 2
    $\begingroup$ @paolo, keep an eye on the In[] values for your inputs. If it resets to In[1] you know the Kernel has quit and restarted. $\endgroup$ Aug 29, 2012 at 20:13

1 Answer 1


Perhaps I misunderstood, but look at how many elements there should have been in your list:

ListLogLogPlot@Table[{i, Length[Permutations[Range[i], {2}]]}, {i,10^Range[1, 3., .2]}] Mathematica graphics

It sounds like your kernel quits when you try to calculate the permutations.

  • 1
    $\begingroup$ Unfortunately, I believe you may be correct. The fact that the symbols gets undefined at the second evaluation could point towards a kernel restart. $\endgroup$
    – paolo
    Aug 29, 2012 at 20:12
  • $\begingroup$ @paolo If memory serves me right, then the number of permutations you are trying to produce is Binomial[10000,2] which is roughly 5*10^7, not counting ordering. It's not very suprising that Mathematica is not happy finding all of them. Just crashing the Kernel is kindof rude though... $\endgroup$
    – sebhofer
    Aug 30, 2012 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.