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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:00:27+00:00 2026-05-22T03:00:27+00:00

I am going through a generator, whats the Pythonic way of determining if the

  • 0

I am going through a generator, whats the Pythonic way of determining if the current element is the first or last element of a generator, given that they need special care?

thanks

basically generating tags, so i have items like

<div class="first">1</div>
<div>...</div>
<div class="last">n</div>

so i would like to keep the last item in loop?

  • 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-22T03:00:28+00:00Added an answer on May 22, 2026 at 3:00 am

    Here’s an enumerate-like generator that skips ahead one; it returns -1 for the last element.

    >>> def annotate(gen):
    ...     prev_i, prev_val = 0, gen.next()
    ...     for i, val in enumerate(gen, start=1):
    ...         yield prev_i, prev_val
    ...         prev_i, prev_val = i, val
    ...     yield '-1', prev_val
    >>> for i, val in annotate(iter(range(4))):
    ...     print i, val
    ... 
    0 0
    1 1
    2 2
    -1 3
    

    It can’t tell whether the generator passed to it is “fresh” or not, but it still tells you when the end is nigh:

    >>> used_iter = iter(range(5))
    >>> used_iter.next()
    0
    >>> for i, val in annotate(used_iter):
    ...     print i, val
    ... 
    0 1
    1 2
    2 3
    -1 4
    

    Once an iterator is used up, it raises StopIteration as usual.

    >>> annotate(used_iter).next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in annotate
    StopIteration
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going through pro asp.net mvc 2.0 framework and it seems that he
When going through some sample code, notice that setting the main Window Title in
I've just finished going through the MvcMusicStore tutorial found here . It's an excellent
I'm going through some new code I just wrote and adding NDoc sytle comments
While I was going through some code sample, I noticed the following attributes which
I am working on a Twitter app. By going through the oAuth documentation and
I have a Page scaffold that I created through rails generate scaffold. I ran
I'm trying to learn what iterators and generators are in python, going through a
I'm currently going through the Lynda Python tutorial and in the section on generators
I was going through JNI tutorial and I came across below line in generated

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.