Iterate through list two items at a time

Dan Bishop danb_83 at yahoo.com
Tue Jan 2 21:30:04 EST 2007


On Jan 2, 7:57 pm, "Dave Dean" <dave.d... at xilinx.com> wrote:
> Hi all,
>   I'm looking for a way to iterate through a list, two (or more) items at a
> time.  Basically...
>
>   myList = [1,2,3,4,5,6]
>
> I'd like to be able to pull out two items at a time...

def pair_list(list_):
    return [list_[i:i+2] for i in xrange(0, len(list_), 2)]




More information about the Python-list mailing list