Get number of items in a list in Python
This post will discuss how to get the total number of items in a Python List.
1. Using len() function
The standard way to get the length of an object is with the built-in function len(). It returns an integer value greater than equal to 0, indicating the total number of items in the object. The object can be a sequence (such as a list, string, tuple, or range), or a collection (such as a dictionary or set). To use the len() function with a list, simply pass the list as an argument. Here is an example of how to use this function:
|
1 2 |
nums = [1, 2, 3, 4, 5] # a list print(len(nums)) # print 5 |
Note that it is not preferable to check whether a sequence or container is empty in boolean contexts using the len() function. The following solution shows the pythonic way to check whether a list is empty by placing the list in a boolean context. This works since a list in a boolean context is treated as False if empty, True otherwise.
|
1 2 3 4 |
nums = [] if not nums: print('The list is empty') |
2. Using __len__() function
The len() function is implemented with __len__, which returns the length of the object. Although not recommended, it can be called directly as:
|
1 2 |
nums = [1, 2, 3, 4, 5] # a list print(nums.__len__()) # print 5 |
According to the official sources, len(x) was chosen over x.len() since:
- Prefix notation just reads better than postfix.
len(x)tells the result is an integer and the argument is some kind of container, whereasx.len()tellsxhas to be some kind of container implementing an interface or inheriting from a class that has a standardlen().
3. Using numpy.size() function
Another way to get the number of items in a list in Python is to use the numpy.size() function from numpy module. Here is an example of how to use this method:
|
1 2 3 4 5 6 |
import numpy as np nums = [1, 2, 3, 4, 5] # a list length = np.size(nums) print(length) # print 5 |
4. Using for loop
Another way to get the number of items in a list is to use the for loop. The idea is to declare a counter variable to store the number of items in the list and increment it by one with each iteration of the loop. This works but it is more verbose than the len() function method. For example:
|
1 2 3 4 5 6 7 |
nums = [1, 2, 3, 4, 5] # a list length = 0 for item in nums: length += 1 print(length) # print 5 |
5. Using sum() function
Finally, we can use a generator expression that yields 1 for each item in the list to get the number of items in the list. Here is an example of how to use this method using the sum() function:
|
1 2 3 4 |
nums = [1, 2, 3, 4, 5] # a list length = sum(1 for item in nums) print(length) # print 5 |
This method works, but it may not be very intuitive, as it uses a mathematical function for a non-mathematical purpose.
That’s all about getting the number of items in a list in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)