Python len() Function

Simplify data analysis in Python with the len() function. Explore how to count elements in lists, tuple, array and more with visual examples.

What is len() function in Python?

Python has a built-in function len() for getting the total number of items in a list, tuple, array, dictionary, or any other iterable.

The len() method takes an argument where you may provide an iterable (like list, tuple, etc.) and it returns the length of the given iterable.

Syntax for using the Python len() function

This is how you may use the len() method of Python:

len(object)

For example, we have a list:

A_list = [5, 10, 15, 20, ‘a’, ‘b’, ‘c’]

This is how you may use the len() method for getting the length of that list:

List_length = len(A_list)

In the length function, you may provide an object that can be a sequence (list, tuple, string, bytes, or range) or collections such as a dictionary.

See the following examples of using this len() method for getting the length of lists, arrays, dictionaries, and tuples.

An example of getting list length

In this example, a list is created with a few items. The items are a combination of numbers and string characters.

This is followed by using the len() method. The returned value is assigned to a variable and its value is displayed by using the print function.

See the code and output of this example:

#An example of len() method for list length

len_list = [1, 5, 9, 'x','y','z', 20, 25]

print ("Number of items in the list = ", len(len_list))

Output:

Number of items in the list = 8

You can see, the length of the list is returned as 8.

An example of using len method in for loop

In this example, a list of strings is created. After that, a for loop is used to get the length of each string item in the list.

Basically, it will not only return the total length of the list but also the length of each string item in the list by using the len() method. Have a look:

#An example of len() method

List_words = ['Python', 'List', 'Length', 'Tutorial']

for word in List_words:

    print(word, "=", len(word))

print ("##################")

print ("Length of the list  = ", len(List_words))

Result:

Python = 6
List = 4
Length = 6
Tutorial = 8
##################
Length of the list = 4

Is len() method specific to the list?

No, by definition, the argument of the len() method may be an object e.g. a string, list, byte, tuple, range, etc. You may even provide a collection as a parameter.

So, by using the len() method you may also get the length or the total number of items in a set, dictionary, frozen set, etc.

The following section shows examples of using len() method in different sequences and collections.

Getting the length of an array example

The following example shows how to get the length of an array. First, an array is created after importing the array module. After that, the len() method is used to get the array length which is displayed by using the print function.

Python Program:

#A Demo of array length

from array import array

arr_len_int=array('b',[10,20,30,40,50])

print("Number of items in Array:", len(arr_len_int))

Result:

Number of items in Array: 5

You can see that the array which type is integer contains five elements.

A len method example with a tuple

See this example where a tuple is created with string elements. The len() method is used to get the number of items in the tuple:

Code:

# Getting length of tuple demo

tup_len = ('A', 'Tuple', 'Example', 'With', 'len()')

print ("Number of items in tuple = ", len(tup_len))

Output:

tuple length

Using len() method with a dictionary

Similarly, you may use the len() method with dictionary collection for getting the total number of items in the dictionary. See the demonstration below:

#A demo of len() with dictionary

dict_dir = {'Taniya': 5092454,'Hiba': 897485475, 'Mina':897454745}

print ("Number of items in dictionary = ", len(dict_dir))

Output:

Number of items in dictionary = 3

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!