# Python | Check if list contains consecutive numbers

Given a list of numbers, write a Python program to check if the list contains consecutive integers.

Examples:

```Input : [2, 3, 1, 4, 5]
Output : True

Input : [1, 2, 3, 5, 6]
Output : False
```

Let’s discuss the few ways we can do this task.

Approach #1 : using `sorted()`

This approach uses `sorted()` function of Python. We compare the sorted list with list of range of minimum and maximum integer of the list and return it.

 `# Python3 Program to Create list  ` `# with integers within given range  ` ` `  `def` `checkConsecutive(l): ` `    ``return` `sorted``(l) ``=``=` `list``(``range``(``min``(l), ``max``(l)``+``1``)) ` `     `  `# Driver Code ` `lst ``=` `[``2``, ``3``, ``1``, ``4``, ``5``] ` `print``(checkConsecutive(lst)) `

Output:

```True
```

Approach #2 : Using `numpy.diff()`

Numpy module provides a function `diff()` that calculate the n-th discrete difference along the given axis. We find the iterative difference of the sorted list and check if it is equal to 1.

 `# Python3 Program to Create list  ` `# with integers within given range  ` `import` `numpy as np ` ` `  `def` `checkConsecutive(l): ` `    ``n ``=` `len``(l) ``-` `1` `    ``return` `(``sum``(np.diff(``sorted``(l)) ``=``=` `1``) >``=` `n)  ` `     `  `# Driver Code ` `lst ``=` `[``2``, ``3``, ``1``, ``4``, ``5``] ` `print``(checkConsecutive(lst)) `

Output:

```True
```

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.