How to find length of list in Python
Finding the length of the list is required in many situations while programming. Don’t worry, python has got your back.
There is a built-in function in python for finding the length of the list – len().
>>>li=[1,2,3,4] >>>len(a) 4
Otherwise, you can use for loop to count the number of elements in the list
li = [ 1 , 2 , 3 , 4 ] #initializing a dummy list count = 0 for item in li: count = count + 1 print ( "Length of the list is " + count )
Programmers always go for len(), since the work can be done in a single line.
Please login to comment
View all comments