I’m a bit confused on how to approach this problem.
I have a large number of lists and am looking to delete lists have contents that do not start with a specific digit.
For example. say I’m looking to only keep lists that have at least one node that start with 4 in the following lists:
['4-7', '2-5', '2-9', '2-32', '2-16', '2-29', '2-13', '2-26', '2-20', '2-23']
['2-32', '2-31', '2-36', '2-28', '2-34', '2-43', '2-41', '2-39']
I should delete the second one and keep the first list, in this case. I know how to detect which ones start with 4 and I can figure out the lists that have that trait but not sure how to mark the other lists for deletion.
What’s the best approach? I have many lists like this(several million) so I am trying to find the least expensive way to do this. I was thinking of creating a list to lists to keep and then comparing it with original list and delete the ones that aren’t on both lists, but I am trying to avoid too many nested for loops.
You can try a list comprehension:
Or a generator expression if you don’t want to have the results in memory all at once:
See it working online: ideone