skip next item in list

Rafael Darder Calvo darder at gmail.com
Mon Jun 11 11:36:42 EDT 2007


On 6/11/07, Andre Engels <andreengels at gmail.com> wrote:
> 2007/6/11, ahlongxp <ahlongxp at gmail.com>:
> > list=('a','d','c','d')
> > for a in list:
> >     if a=='a' :
> >         #skip the letter affer 'a'
> >
> > what am I supposed to do?
>
> There might be better ways to do it, but I would do:
>
> flag_last_a = False
> for a in list:
>    if flag_last_a:
>        flag_last_a = False
>        continue
>    if a=='a':
>        flag_last_a = True
>    # Whatever is done when you don't skip
>
> --
> Andre Engels, andreengels at gmail.com
> ICQ: 6260644  --  Skype: a_engels
> --
> http://mail.python.org/mailman/listinfo/python-list
>
another way:

def skip_after(l):
    i = iter(l)
    for x in i:
        yield x
        while x == 'a':
            x = i.next()

depending on what to do in case of consecutive 'a's, change the
'while' for an 'if'
list(skip_after('spam aand eggs'))
['s', 'p', 'a', ' ', 'a', 'd', ' ', 'e', 'g', 'g', 's']



More information about the Python-list mailing list