What would be the most efficient way (in terms of memory and cpu) to cull items from a big list in Python? Is this a good way?
a = range(0,100000)
a[:] = [item for item in a if item > 10]
The numbers are just an example here. Could also be:
a = ["my", "very", "big", "list"]
a[:] = [item for item in a if item.startswith("b")]
If you actually want a list (and you want to replace your original list in place), you're probably not going to do a whole lot better than what you have with pure python. However, this frequently isn't necessary. Frequently, you just want an iterable object:
generator = (item for item in a if item > 10)
for item in generator:
...
This will be more memory efficient and the performance should be roughly the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With