+ 1
Guys..anybody know why this code is not working properly ?? It's to purify an odd numbers from a list
4 Antworten
+ 7
If you truly wish to modify the list in place, start at the end. That way you modify the already processed portion of the list and your access works fine.
for i in reversed(range(len(x))):
if x[i]%2 == 1:
del x[i]
+ 4
Iterating over a list and removing items from it causes weird behaviour so to avoid that create another one and append elements when it’s even...
https://code.sololearn.com/cED1Vjgunc0x/?ref=app
There’s also a built-in function called filter that filters a list based on a condition...
lis = [1, 2, 3, 3, 4]
lis = list(filter(lambda x: x % 2 != 0, lis))
print(lis)
+ 1
Perfect mr.John ..thanks
0
thanks dear alot 👍🏼