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

how to grab next-in-line integers from list python

So i have these 2 lists:

list1 = [81, 68, 53, 28, 19, 7, 2, 0]

list1 is fine and nothing would need to happen as there’s no numbers (1 up, or 1 below) from any of the nums in list.

list2 = [68, 67, 53, 21, 20, 19, 9, 7, 1, 0]

Whereas in list2 i have (68,67) (21,20,19) & (1,0)

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

How can i have a new list be filled with the "extra"(starting from high) next-in-line numbers?

It may or may not be important but just to point out that the list2 will always be sorted from high to low before reaching the below code.

Here’s what ive got so far:

####################################################
list2 = [68, 67, 53, 21, 20, 19, 9, 7, 1, 0]
numbs_list= []
complete = False
i = 0
# start = 0
# end = len(list2) -1
while complete is False:
    if len(list2) > 1:
        # if index 1 is next-in-line to index 0
        # if 67 == 67(68 -1)
        if list2[i +1] == list2[i] -1:

            # add 68 to numbs list
            numbs_list.append(list2[i])

            # remove 68 from list2
            list2.pop(list2.index(list2[i]))
        else:
            list2.pop(list2.index(list2[i]))
    else:
        complete = True

    # start += 1
    # if start == end:
    #     complete = True

# from list2 this is what i need numbs_list to have stored once the while loop is done:
numbs_list = [68, 21, 20, 1]
# whats left in list2 does not matter after the numbs_list is finalised as list2 will eventually get cleared and repopulated.
####################################################

"next-in-line" may be the wrong wording but i think you get the idea of what i mean. if not here’s some examples:

1,0
11,10,9
23,22
58,57
91,90,89,88

notice how theres no room between any & all of those nums? because theyre all next-in-line.

>Solution :

Try iterating through each index and comparing it to the index – 1. and append the index to the extra list if they are only 1 apart

list2 = [68, 67, 53, 21, 20, 19, 9, 7, 1, 0]
extra = []
for i in range(1,len(list2)):
    if list2[i-1] == list2[i] + 1:
        extra.append(list2[i-1])
print(extra)

Using list comprehension:

list2 = [68, 67, 53, 21, 20, 19, 9, 7, 1, 0]
extra = [list2[i-1] for i in range(1,len(list2)) if list2[i-1] == list2[i]+ 1]
print(extra)

Output

[68, 21, 20, 1]
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