You are currently viewing Find the Length of a List in Python

How to find or get the length of a list in Python? To find the length of a list in Python use the len(), naive, length_hint(), sum(), collections, and enumerate() functions. The len() function can be used to get the length of various built-in Python data structures, including lists, strings, tuples, sets, dictionaries, and more. In this article, I will explain how to get the length of a list by using all these methods with examples.

Advertisements

1. Quick Examples of Finding Length of List

If you are in a hurry, below are some quick examples of how to get the length of a list.


# Below are the quick examples

# Example 1: Get the length of a list 
# using len() method
technology = ['Spark','Pandas','Pyspark','Java','C++']
result = len(technology)

# Example 2: Get the length of a list 
# using naive method
technology = ['Spark','Pandas','Java','C++'] 
count = 0
for i in technology:
    count = count + 1

# Example 3: Get the length of a list 
# using length_hint()
from operator import length_hint 
technology = ['Spark','Pandas','Pyspark','Java','C++']
print("Actual List: ",technology)
result = length_hint(technology)

# Example 4: Get the length of list
# using sum() function
numbers = [ 2, 5, 3, 6, 9, 1, 7]
list_length = sum( 1 for i in numbers)

# Example 5: Using enumerate function
technology=['Spark','Pandas','Pyspark','Java','C++'];tech=0
for i,a in enumerate(technology):
  tech+=1

# Example 6: Get the length of a list using collections
from collections import Counter
numbers = [ 2, 5, 3, 6, 9, 1, 7]
list_len = sum(Counter(numbers).values())

2. Get the Length of a List Using len() Method

You can use the built-in len() function in python to get the length of the list which is a number of elements in a list. you can also use this function with tuple, dictionary, string, or any other collection.


# Get the length of a list using len() method
technology = ['Spark','Pandas','Pyspark','Java','C++']
result = len(technology)
print("Final length of list : " + str(result))

# Output:
# Final length of list : 5

3. Get the Length of a List Using Iterating

You can use the naive method to find the length of a list in Python by iterating the list and incrementing a counter for each element until the end of the list is reached. It is a simple and straightforward approach, but not the most efficient when working with large lists.


# Get the length of a list using naive method
technology = ['Spark','Pandas','Java','C++'] 

# Finding length of list
# using loop
count = 0
for i in technology:
    count = count + 1
print("Get length of list: " + str(count))

# Output:
# Get length of list: 4

4. Get the Length of a List Using length_hint()

You can use the length_hint function as part of the operator module in Python, and it provides an estimate of the number of elements in an iterable object. If the length of the iterable object is known, the length_hint function returns the actual length. However, if the length is not known, the function returns an estimated length.


from operator import length_hint 

# Get the length of a list using length_hint()
technology = ['Spark','Pandas','Pyspark','Java','C++']
print("Actual List: ",technology)
result = length_hint(technology)
print("Final Length of list using length_hint() is : " + str(result))

# Output
# Actual List:  ['Spark', 'Pandas', 'Pyspark', 'Java', 'C++']
# Final Length of list using length_hint() is : 5

4. Get the Length of a List Using sum() Method

You can use iteration inside the sum() function to get the length of a list in Python. For example, the generator expression inside the sum() function iterates through each element in the list and adds 1 to the sum for each iteration. The final sum will be equal to the number of elements in the list, which is the length of the list.


# get the length of list
# using sum() function
numbers = [ 2, 5, 3, 6, 9, 1, 7]
list_length = sum( 1 for i in numbers)
print("Get length of list: " + str(list_length))

# Output
# Get length of list: 7

5. Using enumerate() Function

You can also use the enumerate() function to get the length of the list.


# Using enumerate function
technology=['Spark','Pandas','Pyspark','Java','C++'];tech=0
for i,a in enumerate(technology):
  tech+=1
print("Get length of list: " + str(tech))

# Output
# Get length of list: 5

6. Get the Length of a List Using Collections

Similarly, you can also use the sum() function along with the values() method of a counter object to calculate the total count of elements in the list. For example,


# Get the length of a list using collections
from collections import Counter
numbers = [ 2, 5, 3, 6, 9, 1, 7]
list_len = sum(Counter(numbers).values())
print("Get length of list:", list_len)

# Output
# Get length of list: 7

Conclusion

In this article, I have explained how to find the length of a list in Python by using len(), naive, length_hint(), sum(), collections, and enumerate() functions with examples.

Happy Learning !!