To get the length of a list in Python, we can use the len()
or length_hint()
function. Follow the article to know the specific steps.
Python list length
In Python, ‘list’ is a data type that allows storing various data types inside it and retrieving their values through the position of the elements. In Python, you can say ‘list’ is the most flexible data type.
To get the length of a list, I have the following ways:
Ways To Get the length of a List in Python
Use the len() function
You can use the len() function to get the list’s length. Len() is a built-in function that counts the number of characters in the argument. The argument passed to the function can be a string or a list.
Syntax:
len(obj)
Parameters:
- obj: object to get the length.
Example:
- Create a list.
- Use the len() function to get the length of a list.
myList = ['Tom', 'John', 'Jame', 'JackMa'] # Use the len() function to get the length of list lenOfList = len(myList) print('The length of list : ', lenOfList)
Output:
The length of list : 4
Use the length_hint() method
In Python the ‘operator’ module has a built-in length_hint() function. The length_hint() function returns the length for the ‘obj’ object.
Syntax:
length_hint(obj)
Parameters:
- object: can be string, list, dict, tuple, etc.
Example:
- Create a list.
- Use the length_hint() function to get the length of a list.
- Note: Declare the operator module before using the function.
from operator import length_hint myList = ['Tom', 'John', 'Jame', 'JackMa'] # Use the length_hint() function to get the length of list lenOfList = length_hint(myList) print('The length of list : ', lenOfList)
Output:
The length of list : 4
Use For loop
The last way in this article I want to introduce to you is to use a for loop to get the length of a list.
Example:
- Create a list.
- Use the for loop to the length of a list.
myList = ['Tom', 'John', 'Jame', 'JackMa'] # Create a counter variable lenOfList = 0 for i in myList: # The counter increments as it goes through each element of the list lenOfList = lenOfList + 1 print('The length of list : ', lenOfList)
Output:
The length of list : 4
Summary
Here are the methods that can help you get the length of a list in Python. If you have any questions about this article, please leave a comment below. I will answer your questions. Thank you for reading!
Maybe you are interested:
- Add user input to a list in Python
- Join a list with a newline character in Python
- Get A List Of All Enum Values In Python

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java