Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 576481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:03:06+00:00 2026-05-13T14:03:06+00:00

I need to loop over a list of objects, comparing them like this: 0

  • 0

I need to loop over a list of objects, comparing them like this: 0 vs. 1, 1 vs. 2, 2 vs. 3, etc. (I’m using pysvn to extract a list of diffs.) I wound up just looping over an index, but I keep wondering if there’s some way to do it which is more closely idiomatic. It’s Python; shouldn’t I be using iterators in some clever way? Simply looping over the index seems pretty clear, but I wonder if there’s a more expressive or concise way to do it.

for revindex in xrange(len(dm_revisions) - 1):
    summary = \
        svn.diff_summarize(svn_path,
                          revision1=dm_revisions[revindex],
                          revision2 = dm_revisions[revindex+1])
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:03:06+00:00Added an answer on May 13, 2026 at 2:03 pm

    This is called a sliding window. There’s an example in the itertools documentation that does it. Here’s the code:

    from itertools import islice
    
    def window(seq, n=2):
        "Returns a sliding window (of width n) over data from the iterable"
        "   s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ...                   "
        it = iter(seq)
        result = tuple(islice(it, n))
        if len(result) == n:
            yield result    
        for elem in it:
            result = result[1:] + (elem,)
            yield result
    

    What that, you can say this:

    for r1, r2 in window(dm_revisions):
        summary = svn.diff_summarize(svn_path, revision1=r1, revision2=r2)
    

    Of course you only care about the case where n=2, so you can get away with something much simpler:

    def adjacent_pairs(seq):
        it = iter(seq)
        a = it.next()
        for b in it:
            yield a, b
            a = b
    
    for r1, r2 in adjacent_pairs(dm_revisions):
        summary = svn.diff_summarize(svn_path, revision1=r1, revision2=r2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate over a List of objects, doing something only for the
I have a BOOST_FOREACH loop to iterate over a list. Unfortunately, I also need
I need to loop over a particular list of rows pertaining to a database
I need to loop over a query exactly 12 times to complete rows in
I loop over an array of connection strings and on each loop I extract
for a test I need to make a list with some parameter settings. This
I need to loop through all the matches in say the following string: <a
I need to loop through a set of 2-dimensional array of hidden input fields
i have the following code ..i need to loop through end of the file
I have a number rather large, complex xml documents that I need to loop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.