6

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.

Georgy
  • 12,464
  • 7
  • 65
  • 73
21rw
  • 1,016
  • 1
  • 12
  • 26

2 Answers2

11

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]
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    Possible duplicate of [How to find all occurrences of an element in a list?](http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – McGrady Apr 01 '17 at 09:19
  • 1
    @McGrady, just voted. – falsetru Apr 01 '17 at 09:20
3

You can do that using numpy in the following way:

import numpy as np
a = np.array([2,4,5,2])
np.where(a==a.min())
Miriam Farber
  • 18,986
  • 14
  • 61
  • 76