tags:

views:

563

answers:

5

I would like to loop through a list checking each item against the one following it.

Is there a way I can loop through all but the last item using for x in y? I would prefer to do it without using indexes if I can.

+10  A: 
for x in y[:-1]

If y is a generator, then the above will not work.

Edit For those people downvoting this answer because you think it doesn't answer the question, read the question carefully. Note the sentence which ends in a question mark, that is the curly thing with a dot below it, like this: ?.

If that does not satisfy you, then perhaps the fact David has accepted this as the answer will.

If that doesn't either, then go right ahead.

David: You are welcome to unaccept this answer so I can delete it. This answer is causing my grief.

freespace
this is just pure python love. +1
Perpetualcoder
Unfortunately, this doesn't work is y is an iterator.
Unknown
That is true. Make sure y is a list!
freespace
That answers my question, thanks, but I forgot to ask how I would get the item after x. Is this possible?
David Sykes
I believe an "else" statement will allow you to handle what happens after the loop just don't forget to set x to the last element.
nevets1219
- 1I don't think that answer the question. It is not comparing each item with the next one. – odwl 0 secs ago
odwl
I think I did. Author said he would like to do X, then asked how he can do Y. I answered how he can do Y. That he accepted my answer would indicate I answered the question he asked, if not the question he _really_ wanted to ask.Asker is welcome to demote this answer.
freespace
I'm new to python and this is exactly what I needed.
Jage
+10  A: 

If you want to get all the elements in the sequence pair wise, use this approach (the pairwise function is from the examples in the itertools module).

from itertools import tee, izip, chain

def pairwise(seq):
    a,b = tee(seq)
    b.next()
    return izip(a,b)

for current_item, next_item in pairwise(y):
    if compare(current_item, next_item):
        # do what you have to do

If you need to compare the last value to some special value, chain that value to the end

for current, next_item in pairwise(chain(y, [None])):
Ants Aasma
please note, that use of next for variable name shadows built-in
SilentGhost
I personally don't mind shadowing less used builtins when the scope of the variable is small and the name is good for readability. Nevertheless edited the variable names to maintain good coding practices.
Ants Aasma
+1 This solution is best if you're dealing with potentially long lists.
Carl Meyer
+2  A: 

if you meant comparing nth item with n+1 th item in the list you could also do with

>>> for i in range(len(list[:-1])):
...     print list[i]>list[i+1]

note there is no hard coding going on there. This should be ok unless you feel otherwise.

Perpetualcoder
You could replace len(list[:-1]) with len(list) - 1 to avoid a list copy. And avoid using a variable called list...
Remy Blank
+12  A: 

the easiest way to compare the sequence item with the following:

for i, j in zip(a, a[1:]):
     # compare i (the current) to j (the following)
SilentGhost
this is so much better than mine and pythonic +1
Perpetualcoder
This answers the question I wish I had asked. Thanks
David Sykes
Actually, you can omit the first slice, since zip truncates the longer list to the length of the shorter. This will save you one list creation.(Just in case you are dealing with huge lists. But in that case, you should follow Ants Aasma's approach, which does not copy anything.)
bayer
thanks, usuallyuseless, it does indeed look cleaner.
SilentGhost
why the downvote?
SilentGhost
A: 

To compare each item with the next one in an iterator without instantiating a list:

import itertools
it = (x for x in range(10))
data1, data2 = itertools.tee(it)
data2.next()
for a, b in itertools.izip(data1, data2):
  print a, b
odwl
that's exactly what was suggested by Ants Aasma http://stackoverflow.com/questions/914715/python-looping-through-all-but-the-last-item-of-a-list/914786#914786
SilentGhost