Program to find length of a list without using built-in length() function in Python

Suppose we have a list nums. We have to find the length of this list but without using any length(), size() or len() type of functions.

So, if the input is like nums = [5,7,6,4,6,9,3,6,2], then the output will be 9.

To solve this, we will follow these steps −

  • Solve this by map and list operations
  • x := a list which contains all elements in nums
  • convert all elements in x to 1
  • find sum of x by using sum() method
  • In this example we have used the map() method to convert all into 1 by defining an anonymous function.


Let us see the following implementation to get better understanding −

def solve(nums):
   return sum(map(lambda x:1, nums))
nums = [5,7,6,4,6,9,3,6,2]