I am trying to write a function "detect\_range" which detects ranges of integers from a list let's say:

---

```
a = [2, 4, 5, 6, 7, 8, 10, 12, 13]

```

---

```
import itertools
def detect_range(L):
    for i, j in itertools.groupby(enumerate(L), lambda x: x[1] - x[0]): 
        j = list(j) 
        yield j[0][1], j[-1][1] 
print(list(detect_range(a))) 

```

---

It prints:

---

```
[(2, 2), (4, 8), (10, 10), (12, 13)]

```

---

However, I do not want the single integer like 2 and 10 to be printed in a pair, but single. So the output which I am looking from this code is:

---

```
[2, (4, 9), 10, (12, 14)]

```