The most common and recommended way to find the length of an array (or list) in Python is using the built-in len() function.

my_array = [1, 2, 3, 4, 5]
length = len(my_array)
print(length)  # Output: 5

The len() function is efficient (O(1) time complexity), readable, and works with various data structures like lists, strings, tuples, and dictionaries.

Important distinctions based on array type:

  • Standard Python lists: Use len(list).

  • Arrays from the array module: Use len(array).

  • NumPy arrays: Use len(array) to get the size of the first dimension (e.g., number of rows), or use .size for the total number of elements in the entire array. For multi-dimensional arrays, use .shape to get the dimensions.

While you can use a for loop with a counter or sum(1 for _ in array) to count elements manually, these methods are less efficient (O(n)) and not recommended for simple length checks. The __len__() method is the underlying mechanism called by len(), but you should always use len() for clarity and best practices.

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
🌐
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)....
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4:
Discussions

Difference between len() and array.length -
They are both methods of returning a count of something. This something can be the count of characters in a string, or the number of elements in an array. Which one you use is dependent on the coding language you are using. For example: · But I saw array.length in python as well, it’s also ... More on discourse.codecombat.com
🌐 discourse.codecombat.com
0
April 30, 2020
Why is getting the length of an array O(1) and not O(n)?
Most implementations store the length of an array in the object itself. Whenever you insert/remove it gets updated. Thus you're not iterating to get the length you're just getting a value. More on reddit.com
🌐 r/learnprogramming
45
11
November 7, 2022
Find length of 2D array Python - Stack Overflow
Assuming that all the sublists have the same length (that is, it's not a jagged array). More on stackoverflow.com
🌐 stackoverflow.com
How do I find the length (or dimensions, size) of a numpy matrix in python? - Stack Overflow
It actually works differently than ... for a 2D python array, as this would return 2. So, is there a more intuitive way to find the size of a matrix, or is this the best I have? ... First result for numpy dimensions btw, try the search box ... ... Thanks for pointing me to that question! I did try searching, but "numpy matrix dimensions" (or length or size for ... More on stackoverflow.com
🌐 stackoverflow.com
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.

🌐
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.
🌐
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.
🌐
Real Python
realpython.com › len-python-function
Using the len() Function in Python – Real Python
November 16, 2024 - The function is efficient because it typically retrieves the length from an attribute stored within the object. How can I use len() with different data types in Python?Show/Hide · You can use len() with several built-in data types, including strings, lists, tuples, dictionaries, sets, and range objects. Additionally, len() can be used with third-party data types like NumPy arrays and pandas DataFrames.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-the-length-of-a-list-in-python
How to find the length of a list in Python | DigitalOcean
July 25, 2025 - The most direct, efficient, and “Pythonic” way to get the number of items in a list is by using Python’s built-in len() function. This function is a core part of the language and is designed to be highly optimized for this exact purpose. The len() function is universal and can be used ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-arrays
Python Arrays - GeeksforGeeks
1 week ago - In Python, array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Unlike Python lists (can store elements of mixed types), arrays must have all elements of same type. Having only homogeneous elements makes it memory-efficient.
🌐
Medium
medium.com › @alizek433 › python-array-length-5302bb9d7aa2
Python Array Length. Python Array Length: | by Alize Khan | Medium
February 2, 2024 - The len() function returns the length of the list or array. ... The Python len() method can be used to fetch and display the number of elements contained in the list.
🌐
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.
🌐
CodeCombat
discourse.codecombat.com › t › difference-between-len-and-array-length › 21308
Difference between len() and array.length -
April 30, 2020 - They are both methods of returning a count of something. This something can be the count of characters in a string, or the number of elements in an array. Which one you use is dependent on the coding language you are using. For example: · But I saw array.length in python as well, it’s also ...
🌐
Wyzant
wyzant.com › resources › ask an expert
Getting the length of an array in Python | Wyzant Ask An Expert
May 16, 2018 - Number 1 Python Tutor in Wyzant · About this tutor › · About this tutor › · a= [1,2,3,4,5] len(a) is giving you the length of the list · Upvote • 0 Downvote · Add comment · More · Report · Ask a question for free · Get a free answer to a quick problem.
🌐
Replit
replit.com › home › discover › how to find the length of an array in python
How to find the length of an array in Python | Replit
3 weeks ago - Because Python uses zero-based indexing, a list with five elements has indices from 0 to 4. Trying to access an element at the index equal to the length—for example, my_list[5]—will fail because that index doesn't exist. Always remember that the last valid index in an array is len(array) - 1.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › web development › python
Python Array length - Java Code Geeks
July 28, 2023 - Knowing the length of an array ... tasks. You can use the len() function for general arrays and lists or opt for the numpy library for advanced numerical operations and multi-dimensional arrays....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-initialize-empty-array-of-given-length
Python - Initialize empty array of given length - GeeksforGeeks
As we know Array is a collection of items stored at contiguous memory locations. In Python, a List (Dynamic Array) can be treated as an Array. In this article, we will learn how to initialize an empty array of some given size. Let's see different Pythonic ways to create an empty list in Python with a certain size.
Published   July 12, 2025
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
You refer to an array element by referring to the index number. ... Use the len() method to return the length of an array (the number of elements in an array).