Articles
Screencasts
Exercises
Training
Pastebin
Gift
/
Sign Up
Sign In
Containment checks on lists are slow
Editable Code
""" Containment checks on lists are slow See also https://pym.dev/p/2eb2v/ """ import random from time import perf_counter numbers = [random.randrange(1_000_000) for _ in range(20_000)] start = perf_counter() no_neighbors = [ n for n in numbers if n-1 not in numbers and n+1 not in numbers ] end = perf_counter() print(f"{len(no_neighbors):,} non-neighbors found in {end-start:.3f} seconds")
255 views
Copy
Code copied
Run
in Browser
pym.dev/p/2q8zk/
URL copied
Need to share some Python code?
New Python snippet