FWIW I get significantly different results than @Tim Pietzcker did, using what I think is a more realistic set of input data and using a slightly more rigorous (but otherwise the same) approach to timing for different people's answers.
The names and code snippets are the same as the type, except that I added a variant called Lattyware_rev called Lattyware_rev , which determines which elements to save rather than reject - it turned out to be slower than the previous one. Please note that the two fastest ones do not save order l1 .
Here is the latest timecode:
import timeit setup = """ import random random.seed(42) # initialize to constant to get same test values l1 = [random.randrange(100) for _ in xrange(100)] l2 = [random.randrange(100) for _ in xrange(10)] """ stmts = { "Minion91": """ for x in reversed(l1): if x in l2: l1.remove(x) """, "mgilson": """ for x in l1[:]: # correction if x in l2: l1.remove(x) """, "mgilson_set": """ l1 = list(set(l1).difference(l2)) """, "Lattyware": """ removals = set(l2) l1 = [item for item in l1 if item not in removals] """, "Lattyware_rev": """ keep = set(l1).difference(l2) l1 = [item for item in l1 if item in keep] """, "Latty_mgilson": """ removals = set(l2) l1[:] = (item for item in l1 if item not in removals)""", "petr": """ for item in l2: while item in l1: l1.remove(item) """, "petr (handles dups)": """ l1 = filter(lambda x: x not in l2, l1) """, "millimoose": """ for x in l2: try: while True: l1.remove(x) except ValueError: pass """, "K.-Michael Aye": """ l1 = list(set(l1) - set(l2)) """, } N = 10000 R = 3 timings = [(idea, min(timeit.repeat(stmts[idea], setup=setup, repeat=R, number=N)), ) for idea in stmts] longest = max(len(t[0]) for t in timings)
Output:
fastest to slowest timings of ideas: (10,000 timeit calls, best of 3 executions) len(l1): 100, len(l2): 10) mgilson_set: 0.143126456832 K.-Michael Aye: 0.213544010551 Lattyware: 0.23666971551 Lattyware_rev: 0.466918513924 Latty_mgilson: 0.547516608553 petr: 0.552547776807 mgilson: 0.614238139366 Minion91: 0.728920176815 millimoose: 0.883061820848 petr (handles dups): 0.984093136969
Of course, please let me know if something is radically wrong, which will explain the radically different results.
source share