get range of items of python list
names = ['Alice', 'Bob', 'Tom', 'Grace']
names[1:3]
# Output:
# ['Bob', 'Tom']
get range of items of python list
names = ['Alice', 'Bob', 'Tom', 'Grace']
names[1:3]
# Output:
# ['Bob', 'Tom']
python detect ranges in list
def detect_range(input_list):
start = None
length = 0
for elem in input_list:
# First element
if start is None:
start = elem
length = 1
continue
# Element in row, just count up
if elem == start + length:
length += 1
continue
# Otherwise, yield
if length == 1:
yield start
else:
yield (start, start+length)
start = elem
length = 1
if length == 1:
yield start
else:
yield (start, start+length)
print(list(detect_range(a)))
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us