Python len() function

len() overview

Python len() function returns the number of elements in a given object. It can be used on sequences like strings, bytes, tuples, list, and ranges or collections like dictionaries, sets and frozen sets.

len() on lists

To count the length of a list, use the len() function. It returns its length, that's the count of elements. # count length of list using len() myList = [1, 2, 3, 4, 5] length = len(myList) print(length) The list contains 5 elements and therefore the length 5 is returned. 5

len() on dictionaries

To count the length of a dictionary, use the len() function like used. # count length of a dictionary using len() myDictionary = {'first': 1, 'second': 2, 'third': 3} length = len(myDictionary) print(length) The dictionary contains 3 elements, the length of the dictionary is returned as integer. 3

len() on tuples

To count the length of tuples use the len() function as shown below. # count length of a tuple using len() myTuple = (1, 2, 3, 4, 5) length = len(myTuple) print(length) The length of the tuple is returned. 5 For a nested tuple only the main elements are counted, not the child elements. # count length of nested tuple using len() myNestedTuple = ((1,2), (3,4), (5,6)) length = len(myNestedTuple) print(length) The tuple contains 6 elements overall. The len() functions considers only the elements in the main tuple. So the return value is here 3 instead of 6. 3

len() on strings

If a string is used as argument for the length function, it returns the number of chars in the string. # count number of chars in a string using len() myString = "hello world" length = len(myString) print(length) The string contains 11 characters. The result is returned as integer. 11

len() on empty objects

If the len() function is used on empty objects, it returns the integer 0. # using len() function on empty objects # empty list [] print(len([])) # empty tuple () print(len(())) # empty dictionary {} print(len({})) # empty string '' print(len('')) All elements in this example are empty. The len() function returns 4 times 0 as integer value. 0 0 0 0

How the len() function works

What happens when we call the len() function? The len() function simply calls the __len__() method of a given object and returns its value. To check if an object has the __len__() method by using the hasattr() function. # check if object has attribute __len__ myString = 'hello world' print(hasattr(myString, '__len__')) myString has a length and True is returned. True To access the length of the object, we can directly call the __len__() method instead of using the len() function. myString = 'hello world' # check length of object using __len__() method print(myString.__len__()) # check length of object using len() method print(len(myString)) Both alternatives return the same value: The length of the string is 11 characters. 11 11

len() syntax

The syntax of the len() function is: len(object) <2>len() arguments The len() function accepts a single argument. If it is called with more or less arguments, a TypeError exception is raised. The object can be a string, bytes, list, tuple, dictionary, set, frozen set or a range.

len() return value

The return value of len() is the number of items in the object. In case the object is nested, for example a nested tuple, only the elements in the main object are considered for the count.



Click to jump to section