pythonlist# Find longest consecutive range of numbers in list

I have the following list of numbers:

```
numbers = numbers = [ 1, 3, 11, 12, 14, 15, 16, 3, 4, 6]
```

I would like to get the start and end index of the longest consecutive range of numbers. By consecutive range, I mean an integer range of number without skipping, i.e. 1,2,3,4,5...

This is how I have done it:

```
def get_longest_consecutive_numbers(numbers):
# 1. Split the numbers list into sublists of consecutive numbers
split_list = []
j = 0
for i in range(1,len(numbers)-1):
if numbers[i]+1 is not numbers[i+1]:
split_list.append(numbers[j:i])
j = i
# 2. Find index of longest sublist (of consecutive numbers)
longest_sublist_index = max((len(l), i) for i, l in enumerate(split_list))[1]
# 3. Concatenate all sublists up to this index back together
rest = split_list[:longest_sublist_index]
concat_list = [j for i in rest for j in i]
# 4.
# Start Index: Length of concatenated list
# End Index: Start Index + Length of longest sublist in split_list
start_index = len(concat_list)
end_index = start_index + len(split_list[longest_sublist_index])
return start_index, end_index
```

If I apply my function to the list of numbers:

```
get_longest_consecutive_numbers(numbers)
```

I get:

```
(3, 6)
```

which is correct... but...

**I was wondering if there is a more straight-forward (better) way to do this ?**

Solution

Let's have a bit of fun :

Create the range in which the list is included

Symmetric difference of that with the list

Compute the max distance between two following numbers (gives you the max length)

Get the index of the start and end point

Here is the code :

```
def longest_weird(numbers):
delta = list(set(range(max(numbers))).symmetric_difference(numbers))
start,end = 0,0
maxi = 0
for i,x in enumerate(delta[:-1]):
aux = max(maxi,delta[i+1]-x)
if aux != maxi:
start,end = (x+1,delta[i+1]-1)
maxi = aux
return numbers.index(start),numbers.index(end)
```

- Why when i try to get an array of data that i send from axios, the list split into multiple values (views.py)
- Convert SVG to PNG in Python
- Convert SVG to PNG with Python on Windows
- Server-side SVG to PNG (or some other image format) in python
- How to save pygame Surface as an image to memory (and not to disk)
- Problem of invoke() not working in tkinter
- Combine two dataframes with scalable columns by placing the second dataframe directly below the first dataframe without deleting the keys
- Try to XOR a byte string aginst a single character key
- How to assuredly suppress a DeprecationWarning in Python?
- Plot Python Array
- How do I control number formatting in the python interpreter?
- Formatting thousand separator for integers in a pandas dataframe
- Reshape object with shape (500,)
- I'm coding a Connect 4 game, but every time I drop a disk, it drops the disk in rows not columns
- SWRL rules in OWL 2
- append() function does not work when trying to remove duplicates in json file - Python
- Python string format calling a function
- pyqt4 QTextEdit - How to setMaxLength?
- 'Prophet' object has no attribute 'stan_backend' and there is no answer for me
- How to (intermittently) skip certain cells when running IPython notebook?
- Selenium.py not finding element content
- How can I open the interactive matplotlib window in IPython notebook?
- Pygame level/menu states
- pyFirmata gives error: module 'inspect' has no attribute 'getargspec'
- Combine dictionaries with common and nonexistent keys without filling the gaps
- PyTorch get all layers of model
- Is a Python list's += operator equivalent to append() or extend()?
- Python sorted comparator
- mypy Cannot find implementation or library stub for module
- After changing my discord server to Community Server my own bot doesn't work