def minval(xs):
minum = xs[0]
for i in range(len(xs)):
if xs[i] < minum: #difference in code: xs[i]
minum = xs[i]
return minum
or
def minval(xs):
minum = xs[0]
for i in range(len(xs)):
if i < minum: #difference in code: i
minum = xs[i]
return minum
It gives me the same results:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print minval(lst)
1
Which one is better? Why do they both give the same result?
Is there a better more shorter way to get the same result, without resorting to pythons own functions?
Edit: I got it why it works, I ran the second one, it looped through the "i" which was just the numbers from the range of the len of xs and not the actual indexes of xs.
min
? – Gareth Rees Nov 10 '13 at 12:38