i'm trying to create a simple minimum function by comparing items in a list to each other.

list1 = [4,20,2,19,3254,234,21,03]

for i in list1:

   if list[0] < list[0+i]:

       print i

basically my thought process in pseudocode is

for all items in the list
if item n is less than item n+1
return item n

but i'm not sure how to loop the +1 after every n

so i tried to do it with the i in the for loop

any simple suggestions? i don't want to make this more complicated than it should be

9 Years
Discussion Span
Last Post by woooee

You want to stop at the next to last item in the list, so "+1" will be the last item. Also, do not use "i", "l", or "o" as single letter variable names. They look too much like numbers.

list1 = [4,20,2,19,3254,234,21,03]

stop = len(list1) - 1
low = list1[0]
for x in range(0, stop):
   if list1[x] < list1[x+i]:
       print x, list1[x], list1[x+1]
   if list1[x+1] < low:
      low = list1[x+1]
print "lowest =", low

Thanks. That helps. In your program, i was not defined so i just set it equal to 0 before the loop.


Except I don't think he meant i there, but rather 1 (the number one). I can never be sure though.


Yes that should have been one to get the next value. A good example of why we don't use "i".

Votes + Comments
good example
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.