Python: return the index of the first element of a list which makes a passed function true

前端 未结 6 844
傲寒
傲寒 2020-11-29 04:37

The list.index(x) function returns the index in the list of the first item whose value is x.

Is there a function, list_func_index()

6条回答
  •  情话喂你
    2020-11-29 05:07

    you could do this with a list-comprehension:

    l = [8,10,4,5,7]
    filterl = [a for a in l if a % 2 != 0]
    

    Then filterl will return all members of the list fulfilling the expression a % 2 != 0. I would say a more elegant method...

提交回复
热议问题