## 26. `peelGroup()`: Find the first date group in a list

The argument `pairList` is a list of tuples `(y, c)` such that `y` is a year number and `c` is the number of repetitions of that year number.

The purpose of this function is to find and remove the first date group from `pairList` and return the equivalent textual form. There are three kinds of date groups:

• If `pairList` starts with a sequence of two or more consecutive single years ```[(y, 1), (y+1, 1), (y+2, 1), ..., (yt)]```, remove all of them and return a string of the form `"y–yt"`.

• If `pairList` starts with a tuple `[(y, N)]` and `N > 0`, remove the first tuple and return a string of the form `"y (N)"`.

• If neither of the above cases applies, remove the first element and return its year number converted to string form.

cbchistlib.py
```# - - -   p e e l G r o u p

def peelGroup ( pairList ):
'''Remove one sequence, repeat group, or single value from pairList.

[ pairList is a non-empty sequence of (value, count) tuples such
that the values are ints in ascending order and the
count is an int >= 1 ->
if pairList > 1 ->
pairList  :=  pairList with its first pair removed
return str(pairList) + '(' + str(pairList + ')'
else if pairList starts with a sequence of two or more
pairs such that the values go up by 1 each time and
the counts are never greater than 1 ->
pairList  :=  pairList with those pairs removed
return str(first value) + '-' + str(last value)
else ->
pairList  :=  pairList with the first pair removed
return str(first value) ]
'''
```

First we eliminate the multiply-counted-year case.

cbchistlib.py
```    #-- 1
# [ first  :=  first value from pairList
#   firstCount  :=  first count from pairList
#   pairList  :=  pairList with its first pair deleted ]
first, firstCount = pairList.pop(0)

#-- 2
if firstCount > 1:
return "%d(%d)" % (first, firstCount)
```

Next we check for a sequence of singly-counted years.

cbchistlib.py
```    #-- 3
# [ if pairList starts with a pair (first+1, 1) ->
#       pairList  :=  pairList with all pairs removed that
#           form a sequence (first+1, 1), (first+2, 1), ...
#       return str(first) + '-' + str(last value) ]
if ( ( len(pairList) > 0 ) and
( first+1 == pairList ) and
( 1 == pairList ) ):
last = first
while ( ( len(pairList) > 0) and
( last+1 == pairList ) and
( 1 == pairList ) ):
last, lastCount = pairList.pop(0)
return '%d%s%d' % (first, NDASH, last)
```

If all else fails, we have a singleton.

cbchistlib.py
```    #-- 4
return str(first)
```