Fl1pp
Fl1pp

Reputation: 19

index() function is acting strangely in my code

I'm trying to find the index of a non integer from a list, however when running the script for different values I get different indexes each time! Am I using index() not as intended? I'd appreciate any help.

m = [1, 2, '.', 3]
pos = int()
for y in m:
    if y != int():
        pos = m.index(y)
print(pos)

For this particular list the output prints "3", instead of the wanted "2"

Upvotes: 1

Views: 51

Answers (1)

C. Fennell
C. Fennell

Reputation: 1032

You need to check the type of y and you do not need to initialize pos as an int

m = [1, 2, '.', 3]
for y in m:
    if not isinstance(y, int):
        pos = m.index(y)
print(pos)

Upvotes: 1

Related Questions