Python List Length - len(list)
Python List Length
Python List Length represents the number of elements in the list.
To get the length of list in Python, call the python global function len() with the list passed as argument.
You could also use looping statements, like for loop or while loop, to find the number of elements in a list. But, that would become inefficient, since we have inbuilt function.
Syntax
The syntax of len() function to find the list length is as shown below.
n = len(listname)
The function returns an integer which represents number of items present in the list.
Examples
1. Length of a given list
In the following example, we will create a list and then use the len() builtin function to find the number of items in it.
Python Program
#a list
cars = ['Ford', 'Volvo', 'BMW', 'Tesla']
#find length of list
length = len(cars)
print('Length of the list is :', length)
Output
Length of the list is : 4
There are four elements in the list, hence a length of 4
.
2. Length of a list (with list updates)
In the following example, we will create a list and then add or remove some elements from the list, and find the resulting length.
Python Program
#a list
cars = ['Ford', 'Volvo', 'BMW', 'Tesla']
#some updates on list
cars.append('Honda')
cars.append('Tata')
#find length of list
length = len(cars)
print('Length of the list is :', length)
Output
Length of the list is : 6
Initially, there are four elements in the list. Later we added to elements using list append(). Therefore the final length of the list became six.
Summary
In this tutorial of Python Examples, we learned how to find length of a given list using len() builtin function.