# [✓] Know the number of braces in an expression?

GROUPS:
 An expression may contains many braces, for example a = {1, 2, {3, 4, {5}}} How can I know how many braces are there in an expression by using a function?
5 months ago
9 Replies
 Henrik Schachner 3 Votes Try Depth[a] (* Out: 4 *) 
5 months ago
 Oh，so simple,Thanks very much!!
5 months ago
 Sander Huisman 5 Votes Depth gives the maximum number of indices (+1) you can specify for an expression if you use it with Part. The number of braces are actually given by: 2 Count[a, List, \[Infinity], Heads -> True] two because for every list we have an opening and closing bracket.
5 months ago
 Syd Geraghty 2 Votes Actually the required answer is 3. In[12]:= a = {1, 2, {3, 4, {5}}}; Count[a, List, \[Infinity], Heads -> True] Out[13]= 3 
5 months ago
 Thank you ! But I am not familiar with [Infinity], Heads -> True. Is it the instance of levelspec in Count[expr,pattern,levelspec]? Could you please explain the meanings [Infinity], Heads -> True? How can they serve as the levelspec?
5 months ago
 Themis Matsoukas 2 Votes The FullForm expression for a is List[1,2,List[3,4,List[5]]] The number of opening or closing braces is equal to the number of lists in a: Position[a // FullForm, List] // Length 
 FullForm is redundant because Position already operates on FullForm by default.