Python Performance: Why 'if not list' is 2x Faster Than Using len()

https://lemmy.world/post/28121911

Python Performance: Why 'if not list' is 2x Faster Than Using len() - Lemmy.World

Lemmy

How does Python know of it’s my list or not?

if isinstance(mylist, list) and not mylist

Problem solved.

Or if not mylist # check if list is empty

You’re checking if mylist is falsey. Sometimes that’s the same as checking if it’s empty, if it’s actually a list, but that’s not guaranteed.
Doesn’t Python treat all empty iterables as false tho? This isn’t unique to python, is it? (though I’m not a programmer…just a dude who writes scripts every now and then)
My point is that the second statement you presented can have the effect of evaluating emptiness of a Sequence (note: distinct from an Iterable), but that only holds true if the target of the conditional IS a sequence. I’m underlining the semantic difference that was elided as a result of falsey evaluation.
Ok, help a noob out. What is the difference between a sequence and an iterable? Is a sequence immutable, like a tuple?

An iterable is just something that can be iterated over, like range(10), or [1, 2, 3].

A sequence on the other hand is a Collection that is reversible.

docs.python.org/3/library/collections.abc.html#co…

collections.abc — Abstract Base Classes for Containers

Source code: Lib/_collections_abc.py This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whet...

Python documentation
I know what an iterable is. But I am talking about Type[Iterable], which iirc does not obey falsey eval when empty.