Q:

How do I find the Index of the smallest number in an array in python if I have multiple smallest numbers and want both indexes?

I have an array in which I want to find the index of the smallest elements. I have tried the following method:
distance = [2,3,2,5,4,7,6]

a = distance.index(min(distance))

This returns 0, which is the index of the first smallest distance. However, I want to find all such instances, 0 and 2. How can I do this in Python?

A:

Use np.where to get all the indexes that match a given value:
import numpy as np

distance = np.array([2,3,2,5,4,7,6])

np.where(distance == np.min(distance))[0]

Out[1]: array([0, 2])

Numpy outperforms other methods as the size of the array grows:
Results of TimeIt comparison test, adapted from Yannic Hamann's code below
                     Length of Array x 7
Method               1       10      20      50     100    1000
Sorted Enumerate     2.47  16.291  33.643                      
List Comprehension  1.058   4.745   8.843  24.792              
Numpy               5.212   5.562   5.931    6.22  6.441  6.055
Defaultdict         2.376   9.061  16.116  39.299              

A:

You may enumerate array elements and extract their indexes if the condition holds:
min_value = min(distance)
[i for i,n in enumerate(distance) if n==min_value]
#[0,2]

