How to Find the Length of the List in Python | Python Program
In this article, we will learn to find the length of the list using the python program.
We find the length of the list using two methods:
- Using loops
- Using the len() function
Using loop
Explanation
- We make a variable count, that store the number of the elements present in the list.
- Then, we iterate the list and update the count by +1.
- And lastly, we print the length of the list.
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 | # list list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # count variable to count the number of the elements present in the list count = 0 # iterate the list for i in list: # update the value of the count by +1 count +=1 # print the length of the list print("The length of the list is: ",count) |
Output
The length of the list is: 9
Using len() function
Explanation
- By using len(name_of_list) function we easily find the length of the given list.
- And lastly, we print the length of the list.
Program
1 2 3 4 5 6 7 8 | # list list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # use len() function to find the length of the list length = len(list) # print the length of the list print("The length of the list is: ",length) |
Output
The length of the list is: 9