Get Length of the List using Python len() function

Last updated on: by Digamber
Hello folks, today we are going to learn how to get the length of a list using Python len() function. Python is the most popular programming language these days, and it offers a built-in function to calculate the size of the data in a list, array, tuple etc.

We are going to explore the Python len() function, this function takes a data object as a parameter and returns the number of items in a data list. Here, the important thing is to remember is if you pass a string as a parameter, then the len() method will return the number of the characters of the string.

Understand The Python len Syntax

Let’s discus the Python len() method, this method take single object as an argument, data should be in collection or a sequence form (list, array, tuple etc).


Create an array to find out the length of the list using Python programming language.

data = [7, 15, 33, 40]
Get_List_length = len(data)
print("Python list size will be ", Get_List_length)

Getting Length of The List using Python Example

In the example below, we are getting the length of an array using Python len() method.

We can see, array length is 4 using len() method in python.

Getting the Size of the List and Characters using For Loop in Python

In this part of the tutorial, we are going to calculate the size of the list using the most popular "for" loop. For loop iterates through every value in an array to calculate the array length, it will also calculate the every items length as well with the help of python len() method. Check out the example below.

Tech_List = ['Python', 'Node', 'Express', 'MongoDB']
for tech in Tech_List:
    print(tech, "===", len(tech))
print ("***********************")
print ("List length size  = ", len(Tech_List))

Data Type To Be Passed in len() Method

Now, I am going to share with you data types accepted by len() method as an argument. You can pass an object, string, list, byte, tuple, range, and collection.

Getting the Python Array Length Example

To calculate the length of the array, we need to import the array module then we are declaring an Array() and assigning a single "a" string and numerical values. After that we are printing the result to get the size of the array.

#An example of array length
from array import array
Array = array('a', [5, 15, 20, 30, 40])
print("Total items in an array:", len(Array))

Following will be the output:

# Total items in an array: 5

Getting Dictionary Collection Length using len() Method

You can easily calculate the total number of items in a dictionary collection.

#An example of len() method with dictionary collection
dict = {'Tony Stark': 528938239, 'Captain America': 628938239, 'Thor':728938239}
print ("Total items in dictionary = ", len(dict))
# Output: Total items in dictionary =  3

len() Method Example with Tuple

We are using string values in tuple to get the total number of items in a list using python len() method.

# Getting length of tuple demo
length_tuple = ('Tuple', 'Hello', 'World', 'Example')
print ("Total number of items in a tuple = ", len(length_tuple))
# Total number of items in a tuple =  4


I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.