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.
Syntax – len(list)
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.
Example 1: Length of Python List
In the following example, we will create a list and then use the len() function to find the number of items in it.
#a list cars = ['Ford', 'Volvo', 'BMW', 'Tesla'] #find length of list length = len(cars) print('Length of the list is :', length)
Length of the list is : 4
There are four elements in the list, hence a length of
Example 2: Length of Python 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.
#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)
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.
In this tutorial of Python Examples, we learned how to find length of a Python List.
- Python List of Functions
- How to Get the list of all Python keywords?
- Python Program to Find Duplicate Items of a List
- Python List of Lists
- Python – Convert List to Dictionary
- Python – Count the items with a specific value in the List
- How to Insert Item at Specific Index in Python List?
- Python – Traverse List except Last Element
- How to Reverse Python List?
- Python – List of Strings