How find consecutive missing numbers from list
get_user_input_list = [1,2,3,4,5,7,8,11,12,13,14,15,17,21]
missing_item_in_list = []
start = get_user_input_list[0]
stop = get_user_input_list[-1]
for i in range(start,stop+1):
if i not in get_user_input_list:
missing_item_in_list.append(i)
The ouput i get:
[6,9,10,16,18,19,20]
The ouput i need like:
[[6],[9,10],[16],[18,19,20]]
>Solution :
You need a temporary list that will hold the number if they are consecutives, save it in the main list when there is a break
tmp_list = []
for i in range(start, stop + 1):
if i not in get_user_input_list:
if not tmp_list or tmp_list[-1] + 1 == i:
tmp_list.append(i)
else:
missing_item_in_list.append(tmp_list)
tmp_list = [i]
if tmp_list:
missing_item_in_list.append(tmp_list)