len(mylist) tells me it’s definitely a list and not null or whatever
Do you know what else can tell you it’s a list? Type hinting.
If I care about the distinction between None and an empty list, I’ll make separate checks. len(None) raises an exception, and most of the time that’s not what I want when checking whether there’s something in the list, so not x is generally preferred, especially when type hinting is used consistently enough to be reasonably sure I’m actually getting a list or None. If I get something else, that means things got really broken and they’ll likely get an exception alter (i.e. when doing anything list-like).
For sequence type objects, such as lists, their truth value is False if they are empty.
That’s generally exactly what I want. len(x) == 0 doesn’t tell you it’s a list, it just tells you it has __len__. So that could be a dict, list, or a number of other types that have a length, but aren’t lists.
Don’t rely on checks like that to tell you what type a thing is.
Do you know what else can tell you it’s a list? Type hinting.
If I care about the distinction between None and an empty list, I’ll make separate checks.
len(None)
raises an exception, and most of the time that’s not what I want when checking whether there’s something in the list, sonot x
is generally preferred, especially when type hinting is used consistently enough to be reasonably sure I’m actually getting a list orNone
. If I get something else, that means things got really broken and they’ll likely get an exception alter (i.e. when doing anything list-like).That’s generally exactly what I want.
len(x) == 0
doesn’t tell you it’s a list, it just tells you it has__len__
. So that could be adict
,list
, or a number of other types that have a length, but aren’t lists.Don’t rely on checks like that to tell you what type a thing is.