-5

What is a simple way to use zip to do:

Input: (1,2,3,4,5)
Output: ((1,2),(2,3),(3,4),(4,5))

Edit: yes, general ngram solutions are similar, but too verbose for such a simple task. See answers below to see why.

T3db0t
  • 3,491
  • 4
  • 28
  • 45

4 Answers4

8

zip the tuple with its own tail:

>>> ł = (1,2,3,4,5)
>>> zip(ł, ł[1:])
[(1, 2), (2, 3), (3, 4), (4, 5)]
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    You're gonna need an explicit cast to a tuple or list. – zephyr Jul 15 '16 at 15:26
  • This is the answer I was looking/hoping for. Absolutely elegant. Yes, to strictly satisfy the question it must be cast as a tuple. – T3db0t Jul 15 '16 at 15:27
  • @zephyr Not in Python2 – Bhargav Rao Jul 15 '16 at 15:28
  • Do not call variable `l`, this may be confused with `1`. – Delgan Jul 15 '16 at 15:28
  • @BhargavRao As `zip()` return a list in Python 2 while OP is asking for tuple, I think it should cast it anyway. – Delgan Jul 15 '16 at 15:29
  • 1
    The actual return value of `zip` (whether a list or a generator) isn't that interesting; it's trivial to adapt to another sequence if necessary. – chepner Jul 15 '16 at 15:30
  • 1
    Ah ah, `ł` is pretty but it breaks in Python 2 as unicode cannot be used as variable name. `lst` is juste fine, no need of one-letter. ;) – Delgan Jul 15 '16 at 15:31
  • 2
    My change in variable name is somewhat tongue-in-cheek, and only works in Python 3. But yes, lower-case `l` is a bad variable name :) – chepner Jul 15 '16 at 15:31
  • Python **ZIP** function is more faster then Python **Generator Expression or List Comprehension**. You can check running below Code. I have provide small example too. **python -mtimeit -s't = (1,2,3,5)' 'x=zip(t, t[1:])' = "1000000 loops, best of 3: 0.32 usec per loop "** AND **python -mtimeit -s't = (1,2,3,5)' 'x=tuple((t[i], t[i+1]) for i in range(len(t)-1))' = 1000000 loops, best of 3: 1.25 usec per loop** – Lavanya Pant Jul 15 '16 at 15:40
  • @LavanyaPant: Well, you _should_ expect `zip` to be faster because it does its looping at C speed, which is much faster than a Python `for` loop, even one inside a list comp or generator expression. – PM 2Ring Jul 15 '16 at 15:43
  • @Lavanya: you're testing a generator expression, not a list comprehension. – Eugene Yarmash Jul 15 '16 at 15:44
  • Yes Eugene your right. Thanks ! – Lavanya Pant Jul 15 '16 at 16:08
1

You could initialize the tuple using a list comprehension or generator expression:

>>> x = (1, 2, 3 4, 5)
>>> tuple((x[i], x[i+1]) for i in range(len(x)-1))
((1, 2), (2, 3), (3, 4), (4, 5))

Or using slicing:

>>> tuple(x[i:i+2] for i in range(len(x)-1))
((1, 2), (2, 3), (3, 4), (4, 5))
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0
in = (1, 2, 3, 4, 5)
out = tuple([(in[i], in[i+1] for i in range(len(in) - 1)])
print(out)

>> ((1, 2), (2, 3), (3, 4), (4, 5))
ritlew
  • 1,622
  • 11
  • 13
0

Another possibility

x = (1,2,3,4,5)
tuple([(a,b) for a,b in zip(x[:-1],x[1:])])
zephyr
  • 2,182
  • 3
  • 29
  • 51