my_list = [1,2,3,4,5]
len(my_list)
# 5

The same works for tuples:

my_tuple = (1,2,3,4,5)
len(my_tuple)
# 5

And strings, which are really just arrays of characters:

my_string = 'hello world'
len(my_string)
# 11

It was intentionally done this way so that lists, tuples and other container types or iterables didn't all need to explicitly implement a public .length() method, instead you can just check the len() of anything that implements the 'magic' __len__() method.

Sure, this may seem redundant, but length checking implementations can vary considerably, even within the same language. It's not uncommon to see one collection type use a .length() method while another type uses a .length property, while yet another uses .count(). Having a language-level keyword unifies the entry point for all these types. So even objects you may not consider to be lists of elements could still be length-checked. This includes strings, queues, trees, etc.

The functional nature of len() also lends itself well to functional styles of programming.

lengths = map(len, list_of_containers)
Answer from Soviut on Stack Overflow
Top answer
1 of 8
1261
my_list = [1,2,3,4,5]
len(my_list)
# 5

The same works for tuples:

my_tuple = (1,2,3,4,5)
len(my_tuple)
# 5

And strings, which are really just arrays of characters:

my_string = 'hello world'
len(my_string)
# 11

It was intentionally done this way so that lists, tuples and other container types or iterables didn't all need to explicitly implement a public .length() method, instead you can just check the len() of anything that implements the 'magic' __len__() method.

Sure, this may seem redundant, but length checking implementations can vary considerably, even within the same language. It's not uncommon to see one collection type use a .length() method while another type uses a .length property, while yet another uses .count(). Having a language-level keyword unifies the entry point for all these types. So even objects you may not consider to be lists of elements could still be length-checked. This includes strings, queues, trees, etc.

The functional nature of len() also lends itself well to functional styles of programming.

lengths = map(len, list_of_containers)
2 of 8
54

The way you take a length of anything for which that makes sense (a list, dictionary, tuple, string, ...) is to call len on it.

l = [1,2,3,4]
s = 'abcde'
len(l) #returns 4
len(s) #returns 5

The reason for the "strange" syntax is that internally python translates len(object) into object.__len__(). This applies to any object. So, if you are defining some class and it makes sense for it to have a length, just define a __len__() method on it and then one can call len on those instances.

🌐
W3Schools
w3schools.com › python › gloss_python_array_length.asp
Python Array Length
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... Use the len() method to return the length of an array (the number of elements in an array)....
Discussions

[Python] How to find the length of elements in a list?
Try if len(element) == 2: len returns the number of elements in a list, when applied to a string it returns the number of characters. More on reddit.com
🌐 r/learnprogramming
2
0
November 23, 2016
Help with optimizing fixed-length array code
I seemingly avoided all the lengthy array extensions by initialising an array with the fixed size Growing a list isn't really a problem complexity-wise. It takes amortized O(1) time. Sure, allocating the right number of elements right from the start will save you some constant overhead, but it won't improve the complexity. And for algorithm challenges, you'll usually want to improve the complexity, not the constant factors. Is there something hidden that takes computation time, or a way to optimize it that i do not know? What's taking time is moving (potentially) every element of your array in your insert and delete functions. That's not really hidden though, it's right there in the loops. Inserting into and deleting from sorted arrays, while keeping them sorted, are inherently O(n) operations. You really don't want to use arrays/lists for something like this. If you want a sorted data structure that stays sorted when inserting into and deleting from it, you'll usually want to go for trees. More on reddit.com
🌐 r/learnpython
6
2
May 2, 2024
I've been told this is how you get the array length
I knew someone who genuinely did this to compute an array’s length in Java and he was trying to persuade us that it was faster than just doing “array.length”… More on reddit.com
🌐 r/ProgrammerHumor
119
1738
April 15, 2022
how to write array[1, array.length - 1] in python
Read this: https://stackoverflow.com/questions/509211/understanding-slicing More on reddit.com
🌐 r/learnprogramming
1
0
April 16, 2022
🌐
iO Flood
ioflood.com › blog › get-length-of-array-in-python-guide-and-examples
Get Length of Array in Python: Guide and Examples
March 12, 2024 - The simplest way to determine the length of an array in Python is by using the built-in len() function. Simply pass the array as an argument to the function like so: length = len(array).
🌐
FavTutor
favtutor.com › blogs › find-length-of-array-python
How to Find Length of an Array in Python? (5 Best Methods)
September 19, 2022 - Hence, the last index of an array = length of array - 1 · In Python, an array is not an in-built data type.
🌐
Udacity
udacity.com › blog › 2021 › 10 › how-to-calculate-the-length-of-an-array-in-python.html
How to calculate the length of an array in Python? | Udacity
October 20, 2021 - Therefore, a string’s length is equal to the number of characters within. Take a look at how this works: phrase = "Hello, World!" print("This phrase contains", len(phrase), "characters.") ... This phrase contains 13 characters. This article explored lists, arrays, and NumPy arrays in Python and the benefits of each.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-array-length
Python Array length - GeeksforGeeks
July 23, 2025 - Finding the length of an array in Python means determining how many elements are present in the array. For example, given an array like [1, 2, 3, 4, 5], you might want to calculate the length, which is 5.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Get the dimensions, shape, and size of an array | note.nkmk.me
April 23, 2025 - You can get the number of dimensions, the shape (length of each dimension), and the size (total number of elements) of a NumPy array (numpy.ndarray) using the ndim, shape, and size attributes. The bui ...
🌐
Flexiple
flexiple.com › python › length-of-array-python
Python Array Length - How to Calculate the Length of an Array in Python - Flexiple - Flexiple
April 11, 2022 - For example, consider an array of integers: numbers = [1, 2, 3, 4, 5]. To find the length of this array, you would use the len() function as follows: ... This code will output 5, indicating that the numbers array contains five elements.
🌐
Scaler
scaler.com › home › topics › length of array in python
Length of array in Python - Scaler Topics
May 25, 2022 - Length of an array is the number of elements present in the array. In python, the length of an array is the highest index of an array + 1.
🌐
AskPython
askpython.com › home › how to find the array length in python
How to Find the Array Length in Python - AskPython
April 3, 2023 - Python len() method enables us to find the total number of elements in an array. It returns the count of the elements in an array. ... Here, the array can be any type of array for which we want to find the length.
🌐
IONOS
ionos.com › digital guide › websites › web development › python array length
How to find out the length of a Python array
July 11, 2023 - To use arrays, you need to import other libraries first. In­te­grat­ing NumPy into your project will allow you to use Python arrays. To find out the number of elements in your array, you can use the native Python len function.
🌐
Pierian Training
pieriantraining.com › home › python numpy tutorial: get length of array in python
Python NumPy Tutorial: Get Length of Array in Python - Pierian Training
April 27, 2023 - The easiest and most straightforward way to get the length of a NumPy array is by using the built-in Python function `len()`. This function returns the number of elements in an object, including the elements in a NumPy array.
🌐
Python
docs.python.org › 3 › library › array.html
array — Efficient arrays of numeric values
Array objects support the ordinary mutable sequence operations of indexing, slicing, concatenation, and multiplication. When using slice assignment, the assigned value must be an array object with the same type code; in all other cases, TypeError is raised. Array objects also implement the buffer interface, and may be used wherever bytes-like objects are supported. Raises an auditing event array.__new__ with arguments typecode, initializer. ... The typecode character used to create the array. ... The length in bytes of one array item in the internal representation.
🌐
CodeGym
codegym.cc › java blog › learning python › length of an array in python
Length of an Array in Python
November 7, 2024 - You can use the built-in len() function to easily find the length of a list, numpy array, or array from the array module. Alternatively, you can write your own function using a for loop to count the elements manually.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to get array length in python
How to Get Array Length in Python - Spark By {Examples}
May 31, 2024 - How to get the length of an array in Python? You can get the length of an array using the built-in len() function. The array is a collection of items of
🌐
Codedamn
codedamn.com › news › python
Python Array Size: How to Determine the Length
July 4, 2023 - The length of an array is the number of elements it contains, while the size is the total amount of memory allocated to store the array. In Python, the size of an array can be determined using the itemsize property, which returns the length ...
🌐
DEV Community
dev.to › hrishikesh1990 › how-to-calculate-the-length-of-an-array-in-python-2mng
How to calculate the length of an array in Python? - DEV Community
April 13, 2022 - To find the length of an array in Python, we can use the len() function. It is a built-in Python method that takes an array as an argument and returns the number of elements in the array.
🌐
Medium
medium.com › @alizek433 › python-array-length-5302bb9d7aa2
Python Array Length. Python Array Length: | by Alize Khan | Medium
February 2, 2024 - Length of an array is the number of elements present in the array. In python, the length of an array is the highest index of an array + 1.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python array length
Python Array Length | Working of len() in Python with Examples
May 13, 2024 - In Python, the length of the array is computed using the len() function, which returns the integer value consisting of the number of elements or items present in the given array, known as array length in Python.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai