@abhi9u@lemmy.world to Technology@lemmy.worldEnglish • 4 days agoPython Performance: Why 'if not list' is 2x Faster Than Using len()blog.codingconfessions.comexternal-linkmessage-square112fedilinkarrow-up1175arrow-down123
arrow-up1152arrow-down1external-linkPython Performance: Why 'if not list' is 2x Faster Than Using len()blog.codingconfessions.com@abhi9u@lemmy.world to Technology@lemmy.worldEnglish • 4 days agomessage-square112fedilink
minus-square🌶️ - knighthawklinkfedilinkEnglish7•4 days agoso these are the only 2 ways then? huge if true
minus-square@sugar_in_your_tea@sh.itjust.workslinkfedilinkEnglish1•edit-23 days agoOh, 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 if mylist is None.
so these are the only 2 ways then? huge if true
Oh, 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
.