Python Performance: Why 'if not list' is 2x Faster Than Using len()
Python Performance: Why 'if not list' is 2x Faster Than Using len()
blog.codingconfessions.com Python Performance: Why 'if not list' is 2x Faster Than Using len()
Discover why 'if not mylist' is twice as fast as 'len(mylist) == 0' by examining CPython's VM instructions and object memory access patterns.

You're viewing a single thread.
All comments
111
comments
so these are the only 2 ways then? huge if true
7 0 ReplyOh, there are plenty of other terrible ways:
for _ in mylist: break else: # whatever you'd do if mylist was empty
if not any(True for _ in mylist):
try: def do_raise(): raise ValueError _ = [do_raise() for _ in mylist] except ValueError: pass else: # whatever you'd do i mylist was empty
I could probably come up with a few others as well.
Please note that none of these handles the
TypeError
ifmylist is None
.1 0 Reply
111
comments
Scroll to top