Python-courses-python-exercises-Python-courses- built-inintegrated/DOS-python-module

Exercise 52

Write a Python program as a function which takes as parameters a tuple (L, a) formed by a list L and an element a of L and which returns the list of indexes of a in the list L. Example if L = [2, 7, 11, 7, 21, 39, 7] and a = 7 the function returns [1, 3, 6]

Solution

"""

def listIndex(L , a):
# initialization of list of indexes
lIndex = []
for i in range(0, len(L)):
if L[i] == a:
lIndex.append(i)
return lIndex

# Exemple
L = [2 , 7 , 11 , 7 , 21 , 39 , 7]
a = 7
print(listIndex(L , a)) # output [1, 3, 6]
Younes Derfoufi
my-courses.net

Leave a Reply