+ 1

Guys..anybody know why this code is not working properly ?? It's to purify an odd numbers from a list

https://code.sololearn.com/c0f1dxHTnzf9/?ref=app

22nd Jul 2018, 7:16 PM
Karim
Karim - avatar
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]
22nd Jul 2018, 8:21 PM
John Wells
John Wells - avatar
+ 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)
22nd Jul 2018, 7:24 PM
TurtleShell
TurtleShell - avatar
+ 1
Perfect mr.John ..thanks
24th Jul 2018, 4:43 AM
Karim
Karim - avatar
0
thanks dear alot 👍🏼
22nd Jul 2018, 7:52 PM
Karim
Karim - avatar