Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Python count list length

I want to know how len() works.

Does it count from beginning to end of a list every time I call len(), or, since list is also an class, does len() just return a variable in the list object which record the list's length?

Also, I hope someone can tell me where I can find the source code of those built-in functions like 'len()', 'map()', etc.

like image 533
Robert Bean Avatar asked Apr 27 '13 07:04

Robert Bean


1 Answers

Well, you can find the documentation of the built-in functions here.

The list data-type keeps track of the number of elements it's holding, len(list) is an O(1) operation.


For the source code, you can find the source code of Python at the download page.

like image 56
pradyunsg Avatar answered Oct 14 '22 22:10

pradyunsg