Get Length of List using len() Function in Python

Get Length of List using len() Function 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.

Python Program to Find Length of List using len() Function

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 in python.

# Python program to find length of list using len function

def length_list(l):  #user-defined function
   length = len(l)  #find length of the list
   return length

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

# calling function
length = length_list(list)

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

Output:-

The length of a list is 5

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

Get Length of List using len() Function in Python

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 len() function.

# Python program to find length of list using len function

def length_list(l):  #user-defined function
   length = len(l)  #find length of the list
   return length

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

# calling function
length = length_list(list)

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

Output:-

The length of a list is 4

Python Program to Get Size of List using len() Function

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 length of list using len function

def length_list(l):  #user-defined function
   length = len(l)  #find length of the list
   return length

# take list
list = [5, 'Hi', 12, 'Know', 'Program']

# calling function
length = length_list(list)

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

Output:-

The length of a list is 5

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 *