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 1001427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:40:14+00:00 2026-05-16T07:40:14+00:00

A question of particular interest about python for loops. Engineering programs often require values

  • 0

A question of particular interest about python for loops. Engineering programs often require values at previous or future indexes, such as:

for i in range(0,n):
    value = 0.3*list[i-1] + 0.5*list[i] + 0.2*list[i+1]

etc…

However I rather like the nice clean python syntax:

for item in list:
    #Do stuff with item in list

or for a list of 2d point data:

for [x,y] in list:
    #Process x, y data

I like the concept of looping over a list without explicitly using an index to reference the items in the list. I was wondering if there was a clean way to grab the previous or next item without looping over the index (or without keeping track of the index independently)?

EDIT:

Thanks Andrew Jaffe (and by proxy Mark Byers) and gnibbler for the simple, extendable examples. I wasn’t aware of the itertools or nwise modules till now. John Machin – thanks for the very complex example of what NOT to do. You put a lot of effort into this example, obviously the somewhat recursive algorithm I presented cannot produce a list with the same number of elements as the input list and it presents problems if not using explicit indexes. An algorithm like this would commonly occur in signal processing.

  • 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-16T07:40:15+00:00Added an answer on May 16, 2026 at 7:40 am

    Here’s a recipe, based on the itertools pairwise code, which does general n-wise grouping:

    import itertools
    
    def nwise(iterable, n=2):
        "s->(s_0,s_1, ..., s_n), (s_1,s_2,..., s_n+1), ... "
        ntup = itertools.tee(iterable, n)
        for i, item in enumerate(ntup):
            for ii in range(i):
                next(item, None)
        return itertools.izip(*ntup)
    

    Which can be used thusly:

    >>> import nwise
    >>> ll = range(10)
    >>> for tup in nwise.nwise(ll,3): print tup
    ... 
    (0, 1, 2)
    (1, 2, 3)
    (2, 3, 4)
    (3, 4, 5)
    (4, 5, 6)
    (5, 6, 7)
    (6, 7, 8)
    (7, 8, 9)
    

    [Thanks to Mark Byers’ answer for the idea]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer More description is necessary, but at a first guess, I'd… May 16, 2026 at 11:57 am
  • Editorial Team
    Editorial Team added an answer Yes. Connecting two computers on a LAN is the same… May 16, 2026 at 11:57 am
  • Editorial Team
    Editorial Team added an answer The data you need is stored in the $_SESSION['access_token'] =… May 16, 2026 at 11:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

(EDIT: This question is now outdated for my particular issue, as Google Code supports
I need to identify items of interest nearby to a particular latitude/longitude. I have
This question is about a specific programming problem I am having - I want
I have a question regarding the ||= statement in ruby and this is of
It seems that there has been a recent rising interest in STM (software transactional
I'm currently developing a web application for a particular niche. The point is that
I need my application (Master) to run another application (Worker) on a particular event.
I am trying to create an app where, when you search for a particular
Perhaps this is a stupid question, but I'm trying to use BGL's dijkstra_shortest_paths ,
I'll preface this question by mentioning that while I'm far from a regular expressions

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.