
Python length
How do you get the length of a list or tuple or array in Python?
Let me clarify something at the beginning, by array, you probably mean list in Python. list is the equivalent of arrays in JavaScript or PHP. Arrays in Python is an altogether different thing.
Ok, having cleared that, getting the the size of a list or tuple (or array, if you will), is pretty straighforward. You just call the len function on the object, and there you have it's size. Examples of list and tuple size / lengths are given below.
Size of list
> fighters = ['bruce', 'chuck', 'benny']
> len(fighters)
3
Size of tuple
> movie = ('Terminator', 'James Cameron', 'Arnold Schwarzenegger')
> len(movie)
3
So there it is, you just call the global function len on the list or tuple and you get its size / length.
Did you expect to have something like list.len or tuple.len instead? Actually there is something like that, but it is called list.__len__ or tuple.__len__. And what len really does is, it takes the object and tries to call the objects's __len__ method. So essentially, len works only on objects that has a __len__ method. If you have a custom object that has the __len__ method, you can call len even on that.
So you learnt: to get the size or length of a list or tuple in Python (you probably didn't mean Python array), you just call the len function on the object.
Notes
- If you came from PHP / JavaScript, by array, probably you mean list in Python.
- You can make your custom objects return values to the len function.
Exercise
- Create a custom object which returns weird values for len and shock your family and friends.
- Why isn't there list.len or tuple.len, instead there is list.__len__ or tuple.__len__?
You might also like



![]() |
Master Lock 8413DPF Python Adjustable Locking Cable, 6-Foot x 3/8-inch Home Improvement (Master Lock)
|
![]() |
Master Lock 8419DPF Python Adjustable Locking Cable, Braided Steel, Yellow and Black Contractor Color, 6-Feet x 5/16-inch Home Improvement (Master Lock)
|



![]() |
Python No Spill Clean and Fill Aquarium Gravel Tube, 24-Inch Pet Products (Python Products Inc.)
|
![]() |
Master Lock - Python Trail Camera Adjustable Camouflage Cable Locks 8418KA-2 CAMO Automotive Parts and Accessories (Master Lock)
|
![]() |
Master Lock - Python Adjustable Cable Locks 8413-30 Automotive Parts and Accessories (Master Lock)
|


