Given a list of numbers, write a Python program to check if the list contains consecutive integers.
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
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.
Approach #2 : Using
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.
- Python | N consecutive Odd or Even Numbers
- Python | Consecutive elements pairing in list
- Python | Remove consecutive duplicates from list
- Python | Average of each n-length consecutive segment in a list
- Python | Check if there are K consecutive 1's in a binary number
- Python | Check if a list exists in given list of lists
- Python | Check if a list is contained in another list
- Python | Check if one list is subset of other
- Python | Check if list is sorted or not
- Python | Check whether a list is empty or not
- Python | Check if all elements in a List are same
- Python | Check if a given object is list or not
- Python | Check if all the values in a list are less than a given value
- Python | Numbers in a list within a given range
- Python | Check if list is strictly increasing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.