The len()
function in Python is an inbuilt function that returns the total number of items(length) in an iterable object. This function is very common in use as it can be used to get the length of any iterable object like a string, list, dictionary, etc.
In this tutorial, we are going to learn how the len() function works, and also we will understand how it can be used to find out the length of the list, string, tuples, and dictionaries with the help of examples.
fruits = ['Apple', 'Mango', 'Orange', 'Banana'] # Find the length of the fruits list length = len(fruits) print(length) # Output: 4
Syntax of the len() Function
The syntax of the len()
function is:
len(iterable_object)
The len() Function Parameters
The len()
function takes in a single parameter which can be a sequence like a list, string, tuple, etc., or a collection(dictionary and set). This parameter is required.
Passing a non-iterable object as a parameter will raise a TypeError exception.
The len() Funtion Return Value
The len()
function returns an integer value denoting the total number of items in the given iterable object. If the iterable is empty, it returns 0.
Using Python len() Function to find the length of a List and Tuple
In the example below, we have used the len()
function to find the length of a list and tuple. It counts the total number of items in them and returns as an integer value.
Example 1:
fruits = [] # Empty List print('Length of', fruits, 'is', len(fruits)) fruits = ['apple', 'mango', 'orange', 'banana'] print('Length of', fruits, 'is', len(fruits)) numbers = () # Empty Tuple print('Length of', numbers, 'is', len(numbers)) numbers = (1, 2, 3, 4, 5) print('Length of', numbers, 'is', len(numbers))
Output:
Length of [] is 0 Length of ['apple', 'mango', 'orange', 'banana'] is 4 Length of () is 0 Length of (1, 2, 3, 4, 5) is 5
The len() Function with Strings and Bytes
When we use the len()
function with a string, it returns the total number of characters in it. While using it with a bytes object returns the total number of bytes in that bytes object. The same is true for the bytes array object. See the example below:
Example 2:
fruit = '' # Empty String print('Length of', fruit, 'is', len(fruit)) fruit = 'Apple' print('Length of', fruit, 'is', len(fruit)) language = b'Python' # Bytes Object print('Length of', language, 'is', len(language)) numbers = bytearray([1, 2, 3, 4]) # Bytes Array Object print('Length of', numbers, 'is', len(numbers))
Output:
Length of is 0 Length of Apple is 5 Length of b'Python' is 6 Length of bytearray(b'x01x02x03x04') is 4
The len() Function with Sets and Dictionaries
Using the len()
function with a set returns the total number of items in the set. While using it on a dictionary returns the total number of key-value pairs in that dictionary. See the example below:
Example 3:
numbers = {} # Empty set print('Length of', numbers, 'is', len(numbers)) numbers = {1, 2, 3, 4} print('Length of', numbers, 'is', len(numbers)) student = {} # Empty dictionary print('Length of', student, 'is', len(student)) student = { 'name': 'John', 'age': 24, 'gender': 'Male'} print('Length of', student, 'is', len(student))
Output:
Length of {} is 0 Length of {1, 2, 3, 4} is 4 Length of {} is 0 Length of {'name': 'John', 'age': 24, 'gender': 'Male'} is 3
Frequently Asked Questions
Is len() function or method?
Ans:- len()
is actually a function, not a method. The len()
function calls the inbuilt __len()__
method which returns the length of the object passed to the len() function.
What does __len()__ mean in Python?
Ans:- The __len()__ is an inbuilt python method which is implicitly called by the len() function when calculating the length of the iterable object passed to it.