Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find consecutive missing numbers from list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading