This tutorial will discuss about a unique way to find length of list in Python.
To find the length of a List in Python, we can use the len()
method of Python. It internally calls the __len__()
method of the object which we pass into it. Also, the List has an overloaded implementation of __len__()
method, which returns the count of number of elements in the list.
So basically len()
method will return the number of elements in the list.
Let’s see some examples,
Example 1 : Get the Lenth of List in Python
We have a list of numbers and we will try to get the count of numbers in that list,
Frequently Asked:
Let’s see the complete example,
listObj = [32, 45, 78, 91, 17, 20, 22, 89, 97, 10] # Get the number of elements in list size = len(listObj) print(f'The length of List "listObj" is: {size}')
Output
The length of List "listObj" is: 10
Example 2 : Get the Lenth of List in Python
This is an another example, in which we have a list of strings and we will try to get the number of elements in the list of strings using the len()
method.
Let’s see the complete example,
Best Resources to Learn Python:
listObj = ['This', 'is', 'a', 'very', 'short', 'example'] # Get the number of elements in list size = len(listObj) print(f'The length of List "listObj" is: {size}')
Output
The length of List "listObj" is: 6
Summary
So today we learn how to use len()
method to get the size of a list in Python.
Leave a Reply
You must be logged in to post a comment.