Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Index of multiple minimum elements in a list [duplicate]

Tags:

python

list

I want a list

a = [2,4,5,2]

to give

b = [0, 3]

I know how to do it for when there is only a single min element, but not multiple min elements. example:

b = a.index(min(a))

This will give me b = 0.

like image 836
21rw Avatar asked Apr 01 '17 09:04

21rw


1 Answers

Find the minimum value, then iterate the list with index using enumerate to find the minimum values:

>>> a = [2,4,5,2]
>>> min_value = min(a)
>>> [i for i, x in enumerate(a) if x == min_value]
[0, 3]
like image 69
falsetru Avatar answered Oct 05 '22 23:10

falsetru