def pairs(lst):
i = iter(lst)
first = prev = item = i.next()
for item in i:
yield prev, item
prev = item
yield item, first
Works on any non-empty sequence, no indexing required.
More Related Contents:
- Converting a list of tuples into a dict
- List Element without iteration
- how to unpack a tuple in this form (‘abc’, [20, 20])? [closed]
- Sort a list of tuples by 2nd item (integer value) [duplicate]
- Why can’t I use a list as a dict key in python?
- Concatenating two lists – difference between ‘+=’ and extend()
- Test if lists share any items in python
- Why can tuples contain mutable items?
- How to convert comma-delimited string to list in Python?
- What’s the most Pythonic way to identify consecutive duplicates in a list?
- List vs tuple, when to use each? [duplicate]
- Convert tuple to list and back
- Unpacking a list / tuple of pairs into two lists / tuples
- Is there a better way to iterate over two lists, getting one element from each list for each iteration? [duplicate]
- How to check if all of the following items are in a list?
- Accessing a value in a tuple that is in a list
- Explicitly select items from a list or tuple
- How to convert a list to a list of tuples?
- Transform “list of tuples” into a flat list or a matrix
- How to compare a list of lists/sets in python?
- Tuple or list when using ‘in’ in an ‘if’ clause?
- How to convert nested list of lists into a list of tuples in python 3.3?
- List of Tuples to DataFrame Conversion [duplicate]
- Why can’t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- What is the meaning of list[:] in this code? [duplicate]
- Swap values in a tuple/list inside a list in python?
- How does str.startswith really work?
- Concatenate elements of a tuple in a list in python [duplicate]
- If in Python I put a list inside a tuple, can I safely change the contents of that list? [duplicate]
- Converting string to tuple without splitting characters