122

Given a list

l = [1, 7, 3, 5]

I want to iterate over all pairs of consecutive list items (1,7), (7,3), (3,5), i.e.

for i in xrange(len(l) - 1):
    x = l[i]
    y = l[i + 1]
    # do something

I would like to do this in a more compact way, like

for x, y in someiterator(l): ...

Is there a way to do do this using builtin Python iterators? I'm sure the itertools module should have a solution, but I just can't figure it out.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
flonk
  • 3,726
  • 3
  • 24
  • 37
  • 3
    Although I acceppted sberry's answer, as I asked for a simple builtin-based solution, also consider the elegant and more performant solutions by thefourtheye and HansZauber. – flonk Jan 23 '14 at 09:14

7 Answers7

196

Just use zip

>>> l = [1, 7, 3, 5]
>>> for first, second in zip(l, l[1:]):
...     print(first, second)
...
1 7
7 3
3 5

If you use Python 2 (not suggested) you might consider using the izip function in itertools for very long lists where you don't want to create a new list.

import itertools

for first, second in itertools.izip(l, l[1:]):
    ...
Neuron
  • 5,141
  • 5
  • 38
  • 59
sberry
  • 128,281
  • 18
  • 138
  • 165
44

Look at pairwise at itertools recipes: http://docs.python.org/2/library/itertools.html#recipes

Quoting from there:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

A General Version

A general version, that yields tuples of any given positive natural size, may look like that:

def nwise(iterable, n=2):                                                      
    iters = tee(iterable, n)                                                     
    for i, it in enumerate(iters):                                               
        next(islice(it, i, i), None)                                               
    return izip(*iters)   
Bach
  • 6,145
  • 7
  • 36
  • 61
  • 2
    I like this approach because it doesn't copy the input iterable. For python3, just use `zip` instead of `izip`. – normanius Nov 18 '19 at 11:21
  • How to extend this to also include (sLast, s0)? So instead of yielding n-1 pairs, return n pairs? – normanius Nov 18 '19 at 11:22
  • 1
    @normanius I think the easiest way to extend this is to just pad the end of `iterable` with a copy of the relevant values from its beginning: `nwise(chain(a, islice(b, n-1)), n)` where `a, b = tee(iterable)` – Ryan Tarpine Jan 24 '20 at 17:21
  • For Python 3: https://docs.python.org/3/library/itertools.html#itertools.pairwise – scorpionipx Apr 01 '22 at 09:18
14

I would create a generic grouper generator, like this

def grouper(input_list, n = 2):
    for i in xrange(len(input_list) - (n - 1)):
        yield input_list[i:i+n]

Sample run 1

for first, second in grouper([1, 7, 3, 5, 6, 8], 2):
    print first, second

Output

1 7
7 3
3 5
5 6
6 8

Sample run 1

for first, second, third in grouper([1, 7, 3, 5, 6, 8], 3):
    print first, second, third

Output

1 7 3
7 3 5
3 5 6
5 6 8
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • You can write generator comprehension like this `pair_generator = ((list[i], list[i+1]) for i in range(0, len(list)-1))` – Hieu Doan Apr 03 '20 at 02:48
4

Generalizing sberry's approach to nwise with comprehension:

def nwise(lst, k=2):
    return list(zip(*[lst[i:] for i in range(k)])) 

Eg

nwise(list(range(10)),3)

[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6), (5, 6, 7), (6, 7, 8), (7, 8, 9)]

alancalvitti
  • 476
  • 3
  • 14
3

A simple means to do this without unnecessary copying is a generator that stores the previous element.

def pairs(iterable):
    """Yield elements pairwise from iterable as (i0, i1), (i1, i2), ..."""
    it = iter(iterable)
    try:
        prev = next(it)
    except StopIteration:
        return
    for item in it:
        yield prev, item
        prev = item

Unlike index-based solutions, this works on any iterable, including those for which indexing is not supported (e.g. generator) or slow (e.g. collections.deque).

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
1

You could use a zip.

>>> list(zip(range(5), range(2, 6)))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

Just like a zipper, it creates pairs. So, to to mix your two lists, you get:

>>> l = [1,7,3,5]
>>> list(zip(l[:-1], l[1:]))
[(1, 7), (7, 3), (3, 5)]

Then iterating goes like

for x, y in zip(l[:-1], l[1:]):
    pass
Noctua
  • 5,058
  • 1
  • 18
  • 23
  • 1
    You don't need to trim the end of the first one as zip will only make complete groups. That would be different if you were using `izip_longest`, but then why would you do that. – sberry Jan 23 '14 at 08:50
  • @sberry: You are correct, but I like it better explicit, this way. It's something personal, I guess. – Noctua Jan 23 '14 at 08:51
-2

If you wanted something inline but not terribly readable here's another solution that makes use of generators. I expect it's also not the best performance wise :-/

Convert list into generator with a tweak to end before the last item:

gen = (x for x in l[:-1])

Convert it into pairs:

[(gen.next(), x) for x in l[1:]]

That's all you need.

  • 1
    For `l = [1, 2, 3, 4]` this produces `[(1, 2), (3, 4)]` and not `[(1, 2), (2, 3), (3, 4)]` as requested. It also only works when the list contains an even number of items. – David Pärsson Feb 07 '17 at 15:11
  • Oops you're right. I'm sorry, I shouldn't post crap on the internet w/o testing it. I've corrected it to work now (hopefully) if you were interested in this form of solution anyhow. – Burak Cetin Dec 28 '18 at 18:06