Exercise 65
Given a list of strings, write a Python algorithm allowing which replace the elements of the list with their lengths. Example: if
L = ["Python", "Django", "Numpy", "Sympy"]
, the algorithm returns the list
[6, 6, 5, 5]
Solution
def replaceByLength (L):
# initialization of the searched list
listLength = []
# iterate over the elements of L and replace them with their lengths
for word in L:
listLength.append (len (word))
return listLength
# Example
L = ["Python", "Django", "Numpy", "Sympy"]
print (replaceByLength (L)) # the output is: [6, 6, 5, 5]
Younes Derfoufi
my-courses.net
my-courses.net