How are the latter two better than the former solution?
primes = (1, 2, 3, 5, 7)
# Classic solution
items = list(range(10))
for prime in primes:
items.remove(prime)
items
# List comprehension
items = list(range(10))
[item for item in items if item not in primes]
# Filter
items = list(range(10))
list(filter(lambda item: item not in primes, items))
The three examples are something I came across in a book and it says that the first solutions takes O(n*m) time (n=len(items), m=len(primes)) whereas the latter two take O(n*1) time... Resulting in 50 comparisons for the first solution (slightly better actually - 40 comparisons) and just 10 for the latter.
I do not understand this. I don't understand how could it be time or memory efficient.
Here is the paragraph in the book that explains this:
To remove or insert a single item from/into the list, Python needs to copy the entire list, which is especially heavy with larger lists. When executing this only once, it is of course not all that bad. But when executing a large number of deletions, a filter or list comprehension is a much faster solution because, if properly structured, it needs to copy the list only once. .... then the examples ... The latter two are much faster for large lists of items. This is because the operations are much faster. To compare using n=len(items) and m=len(primes), the first takes O(m*n)=5*10=50 operations, whereas the latter two take O(n*1)=10*1=10 operations.
EDIT:
The book is not wrong. primes = set((1, 2, 3, 5, 7)) is the right declaration and not primes = (1, 2, 3, 5, 7)