How should I grab pairs from a list in python?

Posted by tomaski on Stack Overflow See other posts from Stack Overflow or by tomaski
Published on 2010-05-13T19:59:55Z Indexed on 2010/05/18 13:00 UTC
Read the original article Hit count: 119

Filed under:
|

Say I have a list that looks like this:

['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10']

Using Python, how would I grab pairs from it, where each item is included in a pair with both the item before and after it?

['item1', 'item2']
['item2', 'item3']
['item3', 'item4']
['item4', 'item5']
['item5', 'item6']
['item6', 'item7']
['item7', 'item8']
['item8', 'item9']
['item9', 'item10']

Seems like something i could hack together, but I'm wondering if someone has an elegant solution they've used before?

© Stack Overflow or respective owner

Related posts about python

Related posts about list