Home »
Python
Preferred way to retrieve the length of an array in Python
Finding length of an array: Here, we are going to learn about the preferred way to retrieve the length of an array in Python.
By Sapna Deraje Radhakrishna Last updated : January 14, 2024
Retrieving the length of an array
The __len__() is a method on container types. However, Python also provides another option of retrieving the length of an array, using the method len().
The len(abc) usually ends up calling abc.__len__().
Retrieve the length of an array using __len__()
# array declaration
test_arr = [1,2,3]
# length
print(len(test_arr))
print(test_arr.__len__())
Output
3
3
Retrieve the length of an array using len()
The len() method works on a tuple, string (which are array of characters) in similar way.
test_tuple = (1,2,3)
# length
print(len(test_tuple))
# string
test_str = 'include-help'
# length
print(len(test_str))
Output
3
12