Maybe your solutions work with integers, but it doesn’t work with dictionaries for me.
On the one hand, deleting () does not work for me. But perhaps this works with basic types. I assume the code below is also a way to remove items from a list of objects.
On the other hand, "del" is also not working properly. In my case, using python 3.6: when I try to remove an item from the list using the for command using the del command, python changes the index in the process and bucle stops prematurely before the time runs out. This only works if you delete item by item in reverse order. Thus, you do not change the index of the array of pending elements when you pass it
Then I used:
c = len(list)-1 for element in (reversed(list)): if condition(element): del list[c] c -= 1 print(list)
where 'list' is like [{{'key1': value1 '}, {' key2 ': value2}, {' key3 ': value3}, ...]
You can also do more pythonic using the enumeration:
for i, element in enumerate(reversed(list)): if condition(element): del list[(i+1)*-1] print(list)
David Martínez Nov 23 '18 at 11:44 2018-11-23 11:44
source share