Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if list contains consecutive numbers in Python
Checking if a list contains consecutive numbers is a common task in data analysis. Python provides several approaches to verify if all elements in a list form a continuous sequence when arranged in order.
Using range() and sorted()
This method sorts the list and compares it with a range of consecutive numbers from minimum to maximum value ?
Example
numbers_a = [23, 20, 22, 21, 24]
sorted_list = sorted(numbers_a)
range_list = list(range(min(numbers_a), max(numbers_a) + 1))
if sorted_list == range_list:
print("numbers_a has consecutive numbers")
else:
print("numbers_a has no consecutive numbers")
# Checking again with non-consecutive numbers
numbers_b = [23, 20, 13, 21, 24]
sorted_list = sorted(numbers_b)
range_list = list(range(min(numbers_b), max(numbers_b) + 1))
if sorted_list == range_list:
print("numbers_b has consecutive numbers")
else:
print("numbers_b has no consecutive numbers")
The output of the above code is ?
numbers_a has consecutive numbers numbers_b has no consecutive numbers
Using NumPy diff() and sorted()
The diff() function calculates differences between adjacent elements. For consecutive numbers, all differences should be 1, so their sum equals length minus 1 ?
Example
import numpy as np
numbers_a = [23, 20, 22, 21, 24]
sorted_list_diffs = sum(np.diff(sorted(numbers_a)))
if sorted_list_diffs == (len(numbers_a) - 1):
print("numbers_a has consecutive numbers")
else:
print("numbers_a has no consecutive numbers")
# Checking again with non-consecutive numbers
numbers_b = [23, 20, 13, 21, 24]
sorted_list_diffs = sum(np.diff(sorted(numbers_b)))
if sorted_list_diffs == (len(numbers_b) - 1):
print("numbers_b has consecutive numbers")
else:
print("numbers_b has no consecutive numbers")
The output of the above code is ?
numbers_a has consecutive numbers numbers_b has no consecutive numbers
Using Set Length Comparison
A more efficient approach compares the length of the original list with the range span. For consecutive numbers, these should be equal ?
Example
def has_consecutive_numbers(numbers):
if len(numbers) <= 1:
return True
numbers_set = set(numbers)
min_val = min(numbers_set)
max_val = max(numbers_set)
return len(numbers_set) == (max_val - min_val + 1)
# Test with consecutive numbers
numbers_a = [23, 20, 22, 21, 24]
print(f"numbers_a has consecutive numbers: {has_consecutive_numbers(numbers_a)}")
# Test with non-consecutive numbers
numbers_b = [23, 20, 13, 21, 24]
print(f"numbers_b has consecutive numbers: {has_consecutive_numbers(numbers_b)}")
# Test with duplicates
numbers_c = [1, 2, 2, 3, 4]
print(f"numbers_c has consecutive numbers: {has_consecutive_numbers(numbers_c)}")
The output of the above code is ?
numbers_a has consecutive numbers: True numbers_b has consecutive numbers: False numbers_c has consecutive numbers: True
Comparison
| Method | Time Complexity | Handles Duplicates | Memory Usage |
|---|---|---|---|
| range() and sorted() | O(n log n) | No | O(n) |
| NumPy diff() | O(n log n) | No | O(n) |
| Set length comparison | O(n) | Yes | O(n) |
Conclusion
Use set length comparison for the most efficient solution. The range() method is intuitive for beginners, while NumPy diff() is useful when working with numerical data arrays.
