How to Find the Length of a List in Python Without Len()

How to Find the Length of a List in Python Without Len() | The length of a list is the number of elements in a list, there are many methods to find python list size. The list is a container that has many elements of similar data types.

Python Program to Find the Length of a List without len()

We will find the number of elements in a list without using the len() function. In this program, we have to use the For Loop to find the length of a given list.

Step 1: Read the list.
Step 2: initialize the count to 0.
Step 3: Use the for loop to iterate over the elements and increment count for every element.
Step 4: Print the length of the list.

# Python program to find the length of a list without len

# take list
l = [1, 2, 3, 4, 5]

# find length of the list
count = 0
for i in l:
   count = count + 1

# print length of the list
print("The length of a list is", count)

Output:-

The length of a list is 5

In the above code, our aim is to find a number of elements in the list without using len(). So, we have used a for loop to iterate over the list. The count variable is incremented for every element in the list.

How to Find the Length of a List in Python Without Len()

In the previous program, we will find the length of numbers in a list but in this program, we will find the length of strings in a list without using the len() function.

# Python program to find the length of a list without len

# take list
l = ['Welcome', 'to', 'Know', 'Program']

# find length of the list
count = 0
for i in l:
   count = count + 1

# print length of the list
print("The length of a list is", count)

Output:-

The length of a list is 4

Python Program to Get Size of List Without len()

This python program performs the same task but different input values. In this program, how to find the length of a list in python without len() function. Here, we will give both numbers and strings in a list.

# Python program to find the length of a list without len

# take list
l = ['Hi', 1, 'Know', 'Program', 5, 22]

# find length of the list
count = 0
for i in l:
   count = count + 1

# print length of the list
print("The length of a list is", count)

Output:-

The length of a list is 6

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *