skip next item in list

danmcleran at danmcleran at
Mon Jun 11 17:28:20 CEST 2007

On Jun 11, 8:49 am, ahlongxp <ahlon... at> wrote:
> list=('a','d','c','d')
> for a in list:
>     if a=='a' :
>         #skip the letter affer 'a'
> what am I supposed to do?

You could do this with itertools.ifilter and an predicate (pred) for a
more OO solution. I've created 2 lists, the source list (l) and the
expected answer (ans). Make sure this is what you meant in your
problem statement:

import itertools

l = ['a', 'b', 'c', 'a', 'd', 'e', 'f', 'a', 'g', 'h']
ans = ['a', 'c', 'a', 'e', 'f', 'a', 'h']

class pred(object):
    def __init__(self):
        self.last = None

    def __call__(self, arg):
        result = None

        if self.last == 'a':
            result = False
            result = True

        self.last = arg

        return result

i = itertools.ifilter(pred(), l)

result = list(i)

print result
print ans

assert result == ans

print 'done'

More information about the Python-list mailing list