Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

There are numerous examples whose end result is the removal of empty brackets "{}" and empty lists here, I still can't find an example of simply removing redundant brackets though.

(It's hard for me to believe there isn't already a common solution to this problem. Please point me there if I missed it. As I am new to mathematica I am learning primarily by example so when I ran into this problem I was at a loss of where to even start.The column like formatting is unique to this question, and is for visual aid only. The actual input is at the bottom.)

For example I have this list as INPUT to a new function:

{
{{{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0}}},
{{{1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}}},
{{{2, 5}, {3, 4}, {4, 3}, {5, 2}}},
{{{3, 5}, {4, 4}, {5, 3}}},
{{{4, 5}, {5, 4}}},
{{{5, 5}}, {{5, 5}}}
}

I would like the new function to generate this list as OUTPUT:

{
{{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0}},
{{1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}},
{{2, 5}, {3, 4}, {4, 3}, {5, 2}},
{{3, 5}, {4, 4}, {5, 3}},
{{4, 5}, {5, 4}},
{{5, 5}, {5, 5}}
}


The actual input TO new function:

{{{{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0}}}, {{{1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}}}, {{{2, 5}, {3, 4}, {4, 3}, {5, 2}}}, {{{3, 5}, {4, 4}, {5, 3}}}, {{{4, 5}, {5, 4}}}, {{{5, 5}}, {{5, 5}}}}

The actual output FROM new function:

{{{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0}}, {{1, 5}, {2, 4}, {3, 3}, {4, 2}, {5, 1}}, {{2, 5}, {3, 4}, {4, 3}, {5, 2}}, {{3, 5}, {4, 4}, {5, 3}}, {{4, 5}, {5, 4}}, {{5, 5}, {5, 5}}}
share|improve this question
Your example following "At this point' makes no sense, because expressions like (t,c) have no meaning, nor do expressions enclosed in square brackets. Otherwise, it appears you are asking to apply a replacement rule like //. {a_List->a} to your expressions. Is that what you're looking for? – whuber Feb 25 at 14:44
Did you look at Flatten command. Flatten[%, 1] should help. – s.s.o Feb 25 at 14:45
1  
@s.s.o Flatten will only help if the extra brackets are at a particular level. If the idea is to get rid of extra brackets anywhere in the expression something else will be needed. I think my replacement rule is probably the simplest way. – Mr.Wizard Feb 25 at 14:46
@ Mr.Wizard true but you don't know how many open and closed brackets you should remove as well :) İn this case you choose 2... – s.s.o Feb 25 at 14:50
1  
@s.s.o No, I used //. so that it will keep applying the rule until all extraneous brackets are gone. Probably not the most efficient way, but it should be effective. – Mr.Wizard Feb 25 at 14:51
show 5 more comments

1 Answer

up vote 17 down vote accepted

Starting with:

a = {{{{0, 5}, {1, 4}, {2, 3}, {3, 2}, {4, 1}, {5, 0}}}, {{{1, 5}, {2, 4}, {3, 3}, {4, 
      2}, {5, 1}}}, {{{2, 5}, {3, 4}, {4, 3}, {5, 2}}}, {{{3, 5}, {4, 4}, {5, 3}}}, {{{4, 
      5}, {5, 4}}}, {{{5, 5}}, {{5, 5}}}};

This is probably the simplest:

a //. {x_List} :> x
share|improve this answer
1  
Bastard! I was just about to start working on this. +1 – rcollyer Feb 25 at 14:44
@rcollyer lol -- well at least you weren't just about to post it. :^) – Mr.Wizard Feb 25 at 14:45
It probably would have taken me a couple of seconds, but yeah, that would have been worse. – rcollyer Feb 25 at 14:46
I'll be damned... that was probably the easiest 10 votes you've ever gotten. Btw, congrats on the 1k in list-manipulation. Now just about 20 or so more answers for the gold :) I used to be just 100 votes behind, but you're way ahead now and at my current answering rate, it'll be a long time before I get there – rm -rf Feb 25 at 17:15
@rm-rf I'm still baffled by the things that get a lot of votes. It seems appropriate that eight of the votes on this came after the cap. I think the solution to this is greater use of the Bounty system to reward those special answers that get no attention. Look for a post on Meta about this in the next week. – Mr.Wizard Feb 26 at 2:03
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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