dimanche 13 septembre 2020

For loop with if else skipping last item Python

I am trying to create a list with only the numbers of initial list. I created a code to retrieve the numbers but it skips the last item. I can´t figure out why it does it. Thanks :)

list1=['a13','b23','c33','d43']  #a13=item   a,1,3 = subitems
list2=[]
list3=[]
for item in list1:
    for subitem in item:
        if subitem.isdigit()==True:
            list2.append(subitem)
            if len(list2)<=2:
                join_digit=''.join(list2)
            else:
                list3.append(join_digit)
                list2=list2[-1:]
print(list3)

Output:

['13','23','33']

Expected output:

['13','23','33','43']

Aucun commentaire:

Enregistrer un commentaire