I noted in comments that the simple answer to your question is that Append does not rewrite bad; you need AppendTo.
But here is a more "Mathematica"-like way to consider. Its main disadvantage is that it actually calculates the test for j >= k as well as j < k, and then throws away the result you don't need.
Position[UpperTriangularize[
Outer[(#1[[1]] < #2[[1]] && #1[[2]] < #2[[2]] && #1[[2]] > #2[[
1]]) &, list, list, 1], 1], True, {2}]
(* {{1, 2}, {2, 3}} *)
Some things to note about this solution:
I have used the optional fourth argument of Outer to get Mathematica to see list as a vector of elements (that just happen to be lists) rather than treating it as a matrix. The result of the Outer function is a matrix of True and False values.
I then zero out the ones we don't need using UpperTriangularize, including its second argument, which makes sure the lead diagonal is also zeros.
Finally, I extract the indices (your j and k) for which the value is True using Position. Again note that I've used an option (third) argument to tell Mathematica at what level of the list I should be looking for the True values.
I'm sure there are other solutions that other members could provide.
Follow-up
I suspect the Outer construct is the one you are having trouble understanding, so here are some examples to build up to the version I used.
list=Range[5];
Outer[(#1<#2)&,list,list]//TableForm
False True True True True
False False True True True
False False False True True
False False False False True
False False False False False
Now try a matrix
list2=RandomInteger[{0,10},{5,2}]
(*{{8,8},{2,0},{5,0},{3,4},{10,6}} *)
Here's an example using the fourth argument of Outer, to show how it knits together each row, not each element, to create a n-by-n matrix, where n is the length of list2.
Outer[(#1[[1]]<#2[[2]])&,list2,list2,1]//TableForm
False False False False False
True False False True True
True False False False True
True False False True True
False False False False False
If I just removed that fourth argument, the command above would record an error because it would then be working on the lowest-level elements of the list list2. So instead I would need to write the following.
Outer[(#1<#2)&,list2,list2]
{{{{False,False},{False,False},{False,False},{False,False},{True,False}},
{{False,False},{False,False},{False,False},{False,False},{True,False}}},
{{{True,True},{False,False},{True,False},{True,True},{True,True}},
{{True,True},{True,False},{True,False},{True,True},{True,True}}},
{{{True,True},{False,False},{False,False},{False,False},{True,True}},
{{True,True},{True,False},{True,False},{True,True},{True,True}}},
{{{True,True},{False,False},{True,False},{False,True},{True,True}},
{{True,True},{False,False},{True,False},{False,False},{True,True}}},
{{{False,False},{False,False},{False,False},{False,False},{False,False}},
{{True,True},{False,False},{False,False},{False,False},{True,False}}}}
The result in that case is a tensor with Dimensions {5, 2, 5, 2}.
Appenddoes not rewritebad; you needAppendTo. But I will follow up with a longer answer (in a couple of hours when it's lunchtime) to suggest a better approach than the loop-and-append approach. Are you by chance a new Mathematica user coming from Matlab or Fortran? I've noticed that people coming from those languages tend to use that approach a lot. – Verbeia Oct 01 '15 at 00:06AppendToinstead ofAppendis a quick fix, however there are certainly better ways than loops andAppendTo, but I have a hard time deciding exactly what your program is intended to do. Consider also that e.g.list[[j]] [[1]]can always be written aslist[[j,1]], which appears more readable. – MarcoB Oct 01 '15 at 00:44Appendwhen you should useAppendTo(or directly updatebadviaAppend), there's nothing intrinsically wrong with your code. – ciao Oct 01 '15 at 05:28Append, the actual goal is a subtly hard problem to do efficiently that has application to similar problems/occasional questions. Were there a "reverse a close vote", I'd use it here personally... – ciao Oct 02 '15 at 03:52AppendtoAppendTo, the general problem itself is rather interesting and has clearly attracted very interesting answers. I'm wondering if you would be willing to edit your title and question with more details about the problem itself, so that we can use this as a reference for future users, and the answers will more closely match the question. – march Oct 09 '15 at 17:17