YouTube Icon

Code Playground.

How to Find the Length of a List in Python

CFG

How to Find the Length of a List in Python

Records are one of the most ordinarily utilized information types in Python and are utilized to store assortments of things of a similar sort. 

This article tells the best way to discover the length of a rundown. 

len() Function

Python has an underlying capacity len() that profits the length of a given article. The item can be a rundown , tuple, string, word reference, and so on. 

The punctuation of the len() work is as per the following: 

len(list)

The capacity acknowledges just a single contention. The returned esteem is a whole number that is the quantity of components in the rundown. 

Here is a model: 

capitals = ['Tokyo', 'Sofia', 'London', 'Budapest']

list_len = len(capitals)

print("The list has {0} elements.".format(list_len))
Output

The list has 4 elements.

Using Loop

Another approach to get the length of a rundown is to utilize the for circle. This works by setting up a counter and circling through all the components of the rundown. On every emphasis, the current estimation of the counter factor is increased by one. 

The following is an example code that tells the best way to locate the quantity of components in a given rundown utilizing the for circle: 

capitals = ['Tokyo', 'Sofia', 'London', 'Budapest']
counter = 0

for capital in capitals:
  counter = counter + 1

print("The list has {0} elements.".format(counter))
Output

The list has 4 elements.

This strategy isn't very Pythonic. You ought to consistently favor utilizing the len() work. 

Conclusion

To discover the length of a rundown in Python list, utilize the len() work. 

On the off chance that you have any inquiries or criticism, don't hesitate to leave a remark.




CFG