-2
*In the final list, numbers appear that were not supposed to be there,
for example 103, which is an odd number from the first list.
lista_1 = [10,24,50,103,30,12]
lista_2 = [13, 16, 39, 14, 107, 35]
for elementos in lista_1:
if elementos % 2 == 0:
lista_1.remove(elementos)
for unidades in lista_2:
if unidades % 2 != 0:
lista_2.remove(unidades)
lista_final = lista_1 + lista_2
print(lista_final)
The code resulted in
[24, 103, 12, 16, 14, 35]
If you want the even elements of
lista_1
, why are you using theremove
to remove them from the list? I recommend you do the table test to describe your code and understand what you really did.– Woss
Do not remove items from a list in the same loop that iterates on it: https://answall.com/q/466768/112052
– hkotsubo