Python program to Find Length of List Without len() Method

Python program to Find Length of List Without len() Method

In this article, we will be going to find the length of list without using default len() method of list. By solving such type of simple problems will help you to build up your logic building skills. If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python program to Find Length of List With and Without len() Method




Logic and Explanation

Question - Write a Python program to Find Length of List With and Without len() Method

Python program to Find Length of List Without len() Method - Suppose we are given a list L and we have to find the length of this list. One method is that we can use the len() method which is an in-built Python method.

L = [1,2,3,4,5,"abc",7,"xyz"]
print("Length of list", len(L))

This was the direct method and we all know about it. Now, let's try to find the length of list without using len() method.

To find the length of list without len() method, the logic we will be using is that iterating through list and at each run of the loop we will add 1 to count variable. As the loop will run same number of times as the number of the elements in the list, the final value of count variable will be equal to length of list. 

I hope logic is clear to you. Now, you can open your text editor and try to code this simple program. Complete source code for the program to find the length of list without len() method is given below.


L = [1,2,3,4,5,"abc",7,"xyz"]
count = 0
for x in L:
    count += 1
print("Length of List = ", count)


Output :- 

Length of List = 8


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post