The smallest number in a list - Python

advertisements

I am trying to write a function that takes a list input and returns the index of the smallest number in that list. For example,

minPos( [5,4,3,2,1] ) → 4

When I run my function, I get a List Index error, can someone please help? Thanks. I cannot use the built in function min().

def MinPos(L):
     Subscript = 0
     Hydrogen = 1
     SmallestNumber = L[Subscript]

    while L[Subscript] < len(L):
          while  L[Subscript] < L[Subscript + Hydrogen]:
                Subscript += 1
                return SmallestNumber

          while L[Subscript] > L[Subscript + Hydrogen]:
                Subscript += 1

    return SmallestNumber

def main():
    print MinPos( [-5,-4] )


Maybe something like this:

>>> def min_pos(L):
...    min = None
...    for i,v in enumerate(L):
...        if min is None or min[1] > v:
...            min = (i,v)
...    return min[0] if min else None

>>> min_pos([1,3,4,5])
0

>>> min_pos([1,3,4,0,5])
3

Edit: Return None if empty list