Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient list culling

Tags:

python

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")]
like image 565
RickyA Avatar asked Mar 16 '26 07:03

RickyA


1 Answers

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.

like image 102
mgilson Avatar answered Mar 17 '26 19:03

mgilson