You can use the 'del' keyword. i.e., del listA[i]
.
However, you shouldn't do this for 2 reasons:
- Deleting a random element from a list is not efficient
- You shouldnt mutate the list while you are iterating over it. Here is what happens if I modify the list while iterating over it, notice it doesn't see 2 elements!
>>> a = [1, 2, 3, 4]
>>> for i, x in enumerate(a):
... print(x)
... del a[i]
...
1
3
It would better to create a temporary list, say tmp_listA, append to it, then replace it to the original. Don't modify the original.
Here is what that would look like:
listA = [1,3,10,12,50,52]
listB = []
tmpA = []
for i in range(len(listA)):
val = listA[i]
if val > 10:
listB.append(val)
else:
tmpA.append(val)
listA = tmpA
print(listA, listB)