Home > Enterprise >  How to get index of spsecific values in list - python
How to get index of spsecific values in list - python

Time:12-17

If I have a list:

A = [1,1,1,0,0,1,0]

How can I return the index of any occurence of the number 1?

With this example, how to return the following:

[0, 1, 2, 5] 

This is because the number 1 appears specifically in these parts of the list

CodePudding user response:

You can use a simple list comprehension:

A = [1,1,1,0,0,1,0]

i = [i for i,v in enumerate(A) if v==1]
[0, 1, 2, 5]
  • Related