Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just

    zip(seq, seq[1:])
Won't work with iterator though.


Indeed:

    all(a < b for a,b in zip(seq,seq[1:]))
(Although one might want <= if checking to see if the sequence is sorted, rather than strictly ascending (that is, it can contain matching/repeated elements)).


itertools.islice would work.


No it won't, unless you `tee` an iterator first.


FYI, the official documentation of itertools has its very own pairwise example:

    def pairwise(iterable):
      "s -> (s0,s1), (s1,s2), (s2, s3), ..."
      a, b = tee(iterable)
      next(b, None)
      return izip(a, b)
https://docs.python.org/2/library/itertools.html#itertools.i...


for the record: the official itertools docs have a lot more examples than this one and they're all really worth knowing about if not studying.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: