Python: Do Python Lists keep a count for len() or does it count for each call?
And one more way to find out how it's done is to look it up on Google Code Search look at the source on GitHub, if you don't want to download the source yourself.
static Py_ssize_t list_length(PyListObject *a)
{
return a->ob_size;
}
Don't worry: Of course it saves the count and thus len()
on lists is a pretty cheap operation. Same is true for strings, dictionaries and sets, by the way!