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.
Note
freespace answered my actual question, which is why I accepted the answer, but SilentGhost answered the question I should have asked.
Apologies for the confusion.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
for x in y[:-1]
If y is a generator, then the above will not work.
Method 2
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)
Method 3
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])):
Method 4
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.
Method 5
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
Method 6
This answers what the OP should have asked, i.e. traverse a list comparing consecutive elements (excellent SilentGhost answer), yet generalized for any group (n-gram): 2, 3, … n:
zip(*(l[start:] for start in range(0, n)))
Examples:
l = range(0, 4) # [0, 1, 2, 3] list(zip(*(l[start:] for start in range(0, 2)))) # == [(0, 1), (1, 2), (2, 3)] list(zip(*(l[start:] for start in range(0, 3)))) # == [(0, 1, 2), (1, 2, 3)] list(zip(*(l[start:] for start in range(0, 4)))) # == [(0, 1, 2, 3)] list(zip(*(l[start:] for start in range(0, 5)))) # == []
Explanations:
l[start:]generates a a list/generator starting from indexstart*listor*generator: passes all elements to the enclosing functionzipas if it was writtenzip(elem1, elem2, ...)
Note:
AFAIK, this code is as lazy as it can be. Not tested.
Method 7
I got you. Suppose this is your list categoricals , containing the names of the columns you want to iterate over, except the last column ReversedPayment.
categoricals = ['SeniorCitizen','CollegeDegree','Married','FulltimeJob','ReversedPayment']
Write your for loop like below and this should give you what you are looking for.
for i in categoricals[:-1]: print(i)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0