
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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] print(solve(nums))
Input
[5,7,6,4,6,9,3,6,2]
Output
9
Advertisements