The length of a list is the total number of elements present in the list.
Program to find the length of a list in Python
We will get a list as input from the user and then find the length of the entered list.
This can be done either by using loops and there are some functions in-built in python to perform the task
Example:
Input:
[3, 6, 9, 8, 1]
Output:
5
Method 1: Using loops
We will loop through the list and then count the number of elements present in the list and print it.
Algorithm:
Program to find the length of a list in Python
Output:
Method 1: Using Python's in-built len() method
Python provides a built-in function for finding the length of the list entered by the user, it is len() function.
Syntax:
Returns an integer value which is the length of the list.
Program to find the length of a list
Output:
Method 3: Using length_hint()
Python's operator module provides one more way to find the length of the list. It is the length_hint() method.
length_hint()
This method is used to find the number of elements present in an iterable structure. It is present in the operator module in Python's library.
Syntax:
Program to find the length of the list in Python
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer