code: def _get_level_lengths(index, hidden_elements=None):
      
      sentinel = object()
      levels = index.format(sparsify=sentinel, adjoin=False, names=False)
  
      if hidden_elements is None:
          hidden_elements = []
  
      lengths = {}
      if index.nlevels == 1:
          for i, value in enumerate(levels):
              if(i not in hidden_elements):
                  lengths[(0, i)] = 1
          return lengths
  
      for i, lvl in enumerate(levels):
          for j, row in enumerate(lvl):
              if not get_option('display.multi_sparse'):
                  lengths[(i, j)] = 1
              elif (row != sentinel) and (j not in hidden_elements):
                  last_label = j
                  lengths[(i, last_label)] = 1
              elif (row != sentinel):
                  # even if its hidden, keep track of it in case
                  # length >1 and later elements are visible
                  last_label = j
                  lengths[(i, last_label)] = 0
              elif(j not in hidden_elements):
                  lengths[(i, last_label)] += 1
  
      non_zero_lengths = {
          element: length for element, length in lengths.items() if length >= 1}
  
      return non_zero_lengths
docs: Given an index, find the level length for each element.
  
      Optional argument is a list of index positions which
      should not be visible.
  
      Result is a dictionary of (level, inital_position): span