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.
Related Contents:
- Converting a list of tuples into a dict
- Creating numbered list of output
- How to sort a list/tuple of lists/tuples by the element at a given index?
- What’s the difference between lists and tuples?
- How to merge lists into a list of tuples?
- Why can’t I use a list as a dict key in python?
- How to extract the n-th elements from a list of tuples
- Sort a list of tuples by 2nd item (integer value) [duplicate]
- Test if lists share any items in python
- Are tuples more efficient than lists 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?
- Pythonic way to return list of every nth item in a larger list
- List vs tuple, when to use each? [duplicate]
- Convert a Scala list to a tuple?
- a mutable type inside an immutable container
- How to unzip a list of tuples into individual lists? [duplicate]
- Convert tuple to list and back
- Unpacking a list / tuple of pairs into two lists / tuples
- Append to a list defined in a tuple – is it a bug? [duplicate]
- Is there a better way to iterate over two lists, getting one element from each list for each iteration? [duplicate]
- Modify a list while iterating
- How to check if all of the following items are in a list?
- Accessing a value in a tuple that is in a list
- How do I loop through a list by twos? [duplicate]
- How to convert a list to a list of tuples?
- What is the inverse function of zip in python? [duplicate]
- How to find the maximum value in a list of tuples? [duplicate]
- Explicitly select items from a list or tuple
- Subtracting 2 lists in Python
- List of tuples to dictionary [duplicate]
- Transform “list of tuples” into a flat list or a matrix
- How to compare a list of lists/sets in python?
- Find an element in a list of tuples
- Why is tuple faster than list in Python?
- How to convert list of tuples to multiple lists?
- Python using enumerate inside list comprehension
- What is the meaning of list[:] in this code? [duplicate]
- Why can’t I use a list as a dict key in python? Exactly what can and cannot be used, and why?
- How to transform string of space-separated key,value pairs of unique words into a dict
- How to convert nested list of lists into a list of tuples in python 3.3?
- What’s the difference between lists enclosed by square brackets and parentheses in Python?
- Extract list of attributes from list of objects in python
- List of Tuples to DataFrame Conversion [duplicate]
- How to flatten a tuple in python
- Does Python have an immutable list?
- In what case would I use a tuple as a dictionary key? [closed]
- Converting string to tuple without splitting characters
- Slicing a list into a list of sub-lists [duplicate]