There are the following methods to find a list length in Python.
- Method 1: Using the len() function
- Method 2: Using a for loop
- Method 3: Using the length_hint() function
Method 1: Using the len() function
Use the len() function to get the length of a list in Python. E.g., list_length = len(list). Apart from the len() function, you can also use the for loop and the length_hint() function to get the length of a list.
Syntax
len(list)
Argument
The len() function takes a list parameter in which the number of elements is counted. It returns the number of elements in the list.
Example
listA = ["Jeff", "Bezos", "Is", "Retiring", "from", "Amazon"]
print("The length of the list is:", len(listA))
Output
The length of the list is: 6
The len() method works in O(1) time as a list is an object with a member to store its size.
Is the len() method specific to the list?
No, the len() method is not specific to the list. In the len() method, you may provide the object that can be a sequence (list, tuple, string, bytes, or range) or collection, such as a dictionary.
Using the len() function, you can get the length of the total number of elements in a set or frozenset.
Finding the length of a list of numbers
Use the len() method and pass the list as an argument, and it returns the length to find a length of a list of numbers or integers.
data = [19, 21, 46, 11]
print(len(data))
Output
4
Finding the length of a list of strings
Use the len() method and pass the list as an argument to find a length of a list of strings.
data = ["Millie", "Bobby", "Brown"]
print(len(data))
Output
3
Limitations of using the len() function
- The len() function only works on lists and specific sequence types.
- The len() function does not work on all objects in Python.
Method 2: Using for loop
To find the length of a list in Python using for loop, define and initialize a counter variable count to 0 and increment count variable to get each element in the list. Then, print the count value using the print() function.
char_list = ["Sadie Sink", "Millie Bobby Brown", "Robin", "Nancy"]
count = 0
for characters in char_list:
count = count + 1
print("The length of the list is: ", count)
Output
The length of the list is: 4
Method 3: Using length_hint()
The length_hint() is a built-in operator module’s method that returns an estimate of the number of elements in the given object. The method’s return value will be exact if the given object supports the len() method.
The operator.length_hint() method also works on a string or integer and returns its length.
import operator
char_list = ["Sadie Sink", "Millie Bobby Brown", "Robin", "Nancy"]
print("The length of the list is: ", operator.length_hint(char_list))
Output
The length of the list is: 4
You can see that operator.length_hint() method returns the length of a list.
Python list length: tips and tricks
How to handle empty lists
Check if a list is empty before trying to access its elements.
To check if a list is empty in Python, use the “in operator”.
main_list = []
if main_list:
first_element = main_list[0]
else:
print("The list is empty")
Output
The list is empty
How to handle large lists efficiently
- Finding the length of a large list can be time-consuming and not efficient, so it is best to use this operation only when necessary.
- Use list comprehension instead of explicit loops(for loop or while loop) to create a new list based on the existing list.
- Use the del statement to remove elements from a list instead of creating a new list without those elements.
FAQs
Is len() a method or a function?
The len() is a built-in function in Python. It is not a method of the list object, but it can be used on lists and other sequence types.
Can I use len() on a list with a mix of different data types?
Yes, you can use len() on a list with a mix of different data types.
The len() function will return the number of elements in the list, regardless of the data type of those elements.
Can I use the len() function to find the length of any object in Python?
No, the len() function only works on lists and certain other sequence types in Python. It will not work on all objects in Python.
Is len() the only way to find the length of a list in Python?
No, there are multiple ways to find a length of a list.
We already covered all the ways in this blog post.
That’s it.