-1

I am trying to index the positions of a list of integers to obtain the position of the intergers that are >0.

This is my list:

paying=[0,0,0,1,0,3,4,0,5]

And this is the desired output:

[3,5,6,8]

rdo=paying[paying>0]

and tried:

rdo=paying.index(paying>0)

The output is in both cases

typeerror > not suported between instances of list and int
JamesHudson81
  • 2,215
  • 4
  • 23
  • 42

3 Answers3

1

Use list comprehensions and enumerate built-in function:

paying=[0,0,0,1,0,3,4,0,5]
print([i for i, e in enumerate(paying) if e > 0])

[3, 5, 6, 8]

vurmux
  • 9,420
  • 3
  • 25
  • 45
1

You can use list comprehension.

paying=[0,0,0,1,0,3,4,0,5]
result = [index for index, value in enumerate(paying) if value > 0]
1

Use enumerate:

paying=[0,0,0,1,0,3,4,0,5]
[i for i, e in enumerate(paying) if e > 0]

OR

[paying.index(e) for e in paying if e > 0]

Result:  [3, 5, 6, 8]
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38