I am a novice in Python. I was just wondering if one can find the length of list or tuple in O(1) time. (len() is O(n))
In C, I can achieve similar thing as follows:
int a[] = {1, 2, 3, 4 ,5};
printf("Length of Array a is :: %d\n", sizeof(a)/sizeof(a[0]));
I know that the above concept works on addresses and that is why it is possible in C, while as per my understanding, Python does not deal in addresses. However I still wanted to ask this question for the sake of curiosity.
Calling
len()on a list in Python isO(1). See here.