## Tuesday, May 11, 2021

Solution Exercise 24: index of an element in a list without using the index() method #### Exercise 24

Write a Python algorithm as a function which takes as parameter a couple (L, a) formed by a list L and an element 'a' and which returns the position of the element 'a' in the list L without using the index() method or any other built-in Python method. The function should return -1 if element 'a' is not present in the list L.

#### Solution

``````# coding: utf-8
def indexElement (L, a):
# initialization of the index of a
index = -1
for i in range (0, len (L)):
if L [i] == a:
index = i
break
return index

# Example
L = [2, 11, 7, 4, 3, 7, 13]
print ("the index of a in L is:", indexElement (L, 7)) # print: the index of a in L is: 2
print ("the index of a in L is:", indexElement (L, 5)) # print: the index of a in L is: -1``````

Younes Derfoufi
my-courses.net

## Python Exercises With Solutions

1. Python Arithmetic Exercises
2. Equations & System Of Equations
3. Polynomials and Functions
4. Python Arithmetic Exercises
5. Numpy, Matplolib, Sympy , Scipy - Exercises
6. Python Arithmetic Exercises

Younes Derfoufi

## Sample Text 