How to Find the Length of a List in Python

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

We will see these below Python program examples:–

  • Python Length Of Each Element In List Using len()
  • Find The Length Of a List In Python Without len() Function
  • Python Program To Find The Length Of a List Using Recursion

Python Program to Find the Length of Each Element in a List

To find the length of elements in a list we can use the len() function. The len() is a predefined function in python that allows us to find the length of a list. So, we will see the program to get the length of the list python.

# Python program to find the length of a list

# take list
list = [0,8,7,6]

# find length of the list
len = len(list)

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

Output:-

The length of a list is 4

The above code finds the length of a list in the len() function which takes the list as a parameter.

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

In the previous program, we used the len() function but in this python program, we will find the number of elements in a list without using the len() function.  Find the Length Of a List in Python using For Loop, we have used a 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.

# Python program to find the length of a list

# take list
n = [1,2,3,4]

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

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

Output:

The length of a list is 4

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.

Python Program to Find the Length of a List using Recursion

The process of calling the same function, again and again, is called recursion, this reduces the length of the code and also makes it easy for the programmer. Let us see the code to find the size of a list in python.

# Python program to find the length of a list

len = 0
def length(a):
   global len
   if a:
      len = len + 1
      length(a[1:])
   return len

# take list
a = [10, 90, 45, 87, 1, 2]

# calling function
len = length(a)

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

Output:

The length of a list is 6

Also See:- How to Swap Elements in List of Python

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 *