![]() |
Python len() method is the most common and widely used method for getting the length of a list in Python. But we can use other methods as well for getting the length or size of the list. In this article, we will see how to find the length of a list in Python without using the len() function. Find The Length Of A List In Python Without Using Len()Below are some of the ways by which we can find the length of a list in Python without using the len() function in Python:
Find The Length of a List Using LoopWe can iterate through the list and count the elements using a loop. Python3
Output
The length of the list is : 6 Get Length of a List In Python Without Using Len()Similar to the for loop, we can use a while loop to iterate through the list and count the elements. Python3
Output
The length of the list is : 7 Find The Length of a List Using RecursionWe can also find the length of list using recursion. In this example, we have used recursion to find the length of a list. Python3
Output
The length of the list is : 7 Length of a List Without Using Len() Using List ComprehensionWe can use a list comprehension to create a new list with the same elements and then find the length of the new list. Python3
Output
Length of the list: 5 |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |