-1

I don't understand what is wrong in this list

L = [20,5,4,85,96,75,3,3.1]
m = min(i for i in L if L.index(i)%2==1)

print L.index(m), m

the output as I expected:

7 3.1

but when I change the last number in list 3.1 to 3

L = [20,5,4,85,96,75,3,3]
m = min(i for i in L if L.index(i)%2==1)

print L.index(m), m

the output was not 3:

1 5

please explain the reason

pzp
  • 6,249
  • 1
  • 26
  • 38
Redhwan
  • 927
  • 1
  • 9
  • 24

1 Answers1

2

You can use enumerate(), to get position for each value in the list:

L = [20,5,4,85,96,75,3,3]
m = min(j for (i,j) in enumerate(L) if i%2==1)

Output:

6 3
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47