How to Find the Length of List in Python using For Loop

How to Find the Length of List in Python using For Loop | 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 using For Loop

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

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 using for loop

# take list
l = [5, 6, 7]

# 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 3

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 List in Python using For Loop

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 using the For Loop.

# Python program to find the length of a list using for loop

# take list
l = ['Hi', '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 5

Python Program to Get Size of List using For Loop

This python program performs the same task but different input values. Here, we will give both numbers and strings in a list.

# Python program to find the length of a list using for loop

# take list
l = [3, 5, 'Hello', 7, 'Know', 'Program', 10]

# 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 7

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 *