Python-courses-download-Python-courses-s-Python-courses

Exercise 6

Write a Python algorithm that returns the length of a given list without using the len() method.

Solution

def lenList (L):
# initialize the length of the list
l = 0
# browsing through all list elements
for x in L:
# length increment
l = l + 1
return l

# Example
L = [11, 3, 5, 8, 2]
print ("the length of the list is:", lenList (L))
# The output is: the length of the list is: 5

Younes Derfoufi
my-courses.net

Leave a Reply