In Python, a list is a built-in data structure used to store multiple elements in a single variable. Sometimes, while working with lists, we need to determine how many elements are present in them. Python provides the built-in len() function to find the length of a list easily. It returns the total number of elements present in the list, including duplicate values and elements of different data types.
How to Find the Length of a List in Python?
In Python, the length of a list can be found using the built-in len() function. It returns the total number of elements present in a list as an integer.
For example, if a Python list contains [10, 20, 30, 40, 50], the len() function returns 5 because the list contains five elements.
Python also allows you to calculate the length of a list using a for loop and a counter variable.
numbers = [10, 20, 30, 40, 50]
print(len(numbers))
Output:
5
Syntax of the len() Function in Python
The len() function is a built-in Python function used to determine the number of items in an object, such as a list, tuple, string, dictionary, or set.
Syntax:
len(object)
Parameters:
– object: The list or other supported object whose length you want to calculate.
Return value:
– Returns an integer representing the total number of elements in the given object.
Python code to print length of an array(list)
# Create a list
numbers = [10, 20, 30, 40, 50]
# Calculate the length of the list
length = len(numbers)
# Print the result
print("Length of the list:", length)
Output:
Length of the list: 5
Explanation:
- First, we create a list named
numberscontaining five elements. - The
len(numbers)function calculates the total number of elements in the list. - The returned value is stored in the
lengthvariable. - Finally, the
print()statement displays the length of the list.
Find the Length of a List Without Using len()
You can also calculate the length of a Python list without using the built-in len() function. To do this, use a for loop and a counter variable.
Python Program for finding length of list:
# Create a list
numbers = [10, 20, 30, 40, 50]
# Initialize counter
count = 0
# Count elements using for loop
for item in numbers:
count += 1
# Print the length
print("Length of the list:", count)
Output:
Length of the list: 5
Explanation:
- Create a list containing five elements.
- Initialize a counter variable
countwith0. - Use a
forloop to iterate through every element in the list. - Increase the counter by one for each element.
- After the loop finishes, the counter contains the total number of elements.
Therefore, the length of the list is 5.
Time Complexity: O(n), because the loop visits every element in the list.
Find the Length of an Empty List in Python
An empty list is a list that does not contain any elements. In Python, you can find its length using the len() function.
Python Program:
# Create an empty list
numbers = []
# Find the length
print("Length of the list:", len(numbers))
Output:
Length of the list: 0
Explanation:
The given list is empty, so it contains zero elements. Therefore, the len() function returns 0.