Python len() Function

Python Len() Function

This is a detailed tutorial of the Python len() Function. Learn to find out the number of items contained in a Python Object using this built-in method.

Python len() Function

The built-in function len() is used to find out the number of items or elements contained in a Python Object. For some Pythons, rather than saying finding the number of items, you can use this function actually to calculate the length of the object. This is one of the most commonly used functions in almost every other python program.

Syntax

len() Parameters

This function len() takes a single argument that is the sequence type object whose length or the number of items you actually want to find out. Make sure that the object that you are passing as an argument to this method is a sequence type object. String, Bytes, List, Dictionary, Tuple, Set, Frozen Set, etc. are the examples for sequence type object data types.

len() Return Type

The return type of the len() function is obviously a positive integer that is basically the count of the items in the sequence object provided as an argument to this function. If the argument provided to this function is not a valid sequence object, this method will raise a TypeError exception. You can find out more about Exception Handling in Python here.

Examples

The different examples given below will help you better understand the len() function, and its working for different python objects.

Example 1. len() Method For Collection Objects (List, Tuple, and Range)

Here we’re finding out the number of items contained in different collection type objects, namely, List, Tuple, and Range.

This function will return zero (0) if the collection object is empty. You can clearly observe in the output that this method returns the exact integer count of the items contained in the list, tuple, and range object.

Len() Function Example For Python Collection Objects (List, Tuple, Range)

Example 2. Calculating Length of String and Bytes Objects using len() Method

For a string or bytes type object, the len() function will return the count for the total number of characters contained in the string or byte type object.

Python Len() Method Strings & Byte Object Example

Example 3. Finding Length of Dictionary and Set Objects using len() Function

Python Len() Function Example For Dictionary, Set And FrozenSet

Example 4. Working of len() Function For Non-Sequence or Custom Objects

Not all Python Objects are sequence objects. If you will pass a Non-Sequence type object as an argument to this function, you will see a TypeError exception raised.

Whenever you apply the len() method for an object, it simply calls the __len__() method, which is actually written inside the definition of the class of that object. The python objects like String, Bytes, List, Tuple, Dictionary, Set, etc. all have the __len__() method defind in their corresponding classes. That method actually returns the length or count of the elements which is further returned by the len() method.

Therefore, if you are building a custom object by defining a custom class, you must specify the __len__() method inside the class definition for the len() method to work for the objects of that class. If you don’t define this method inside the class definition, the following error will be raised.

TypeError: object of type '<Class-Name>' has no len()

This is also illustrated in the following example.

Python Len() Function For Custom Class Objects

The above screenshot clearly specifies th exception raised as the TestClass has not __len__() method defined.

Now, carefully observe the code for the following example.

Now the TestClass contains the method __len__(). Here we’ve defined this function to return the integer count provided as an argument to the object of this class. Therefore, you can clearly see in the output that the len() method applied for the TestClass Objects returns the exact same integer number, which was provided as the argument to the class objects.

This way, you can define your custom Python objects to return whatsoever value for the len() method as per your own logic.

Python Custom Class With Len () Method Example For Len() Method

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *