# What is the best way to completely remove brackets in parts ofan expression

Posted 8 years ago
6709 Views
|
3 Replies
|
1 Total Likes
|
 HelloA simple example :  data = {a, b, c, d}; {15, With[{k = c}, Flatten[Position[data, k]]]} (* output {15,{3}} *) To remove the brackets around 3 I found no other way than: {15, With[{k = c}, Flatten[Position[data, k]]] /. {a_} -> a} (* output {15,3} *) Are there better ways to get this result? The most frequent use I have for this construction is to get iterators values for Sum or Product that are returned by various functions and of course they are always between brackets.Thanks
3 Replies
Sort By:
Posted 8 years ago
 I like. Thanks
Posted 8 years ago
 In[1]:= (* in your example *) {15, {3}} /. {a_, {b_}} -> {a, b} Out[1]= {15, 3} In[2]:= (* more generally *) range = {-2, 3}; Sum[x^2, {x, Sequence @@ range}] Out[3]= 19 
Posted 8 years ago
 If you are sure that c only occurs once, you can do it also with data = {a, b, c, d}; Flatten[{15, With[{k = c}, Position[data, k]]}] or with data = {a, b, c, d}; {15, With[{k = c}, Position[data, k]][[1, 1]]}