Reputation: 1099
I have a list that is very large 1000+ and I am wanting to remove the first 319 elements from the list. I have tried
for i in range(0,320):
list1.pop(i)
but this doesn't work however when I do list1.pop(0)
separately it does remove an element
How am i able to remove the first 319 elements
Upvotes: 0
Views: 213
Reputation: 48071
Use slicing syntax:
del list1[0:319]
By the way, calling list1.pop
repeatedly does not work because the items are reindexed after each deletion. So, when you delete the first item, the next item (that was the second one) becomes the first. If you really want to use pop
in a loop, you have to call list1.pop(0)
319 times - but that's gonna be terribly inefficient.
Upvotes: 2
Reputation: 1124228
Use del
on a slice:
del list1[:319]
This will remove elements 0 - 318 (so a total of 319 elements) in one go.
Upvotes: 2