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

  • Home
  • SEARCH
  • 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 9003619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:44:07+00:00 2026-06-16T00:44:07+00:00

I implemented a (simple) python generator. Now, I want to make another one (from

  • 0

I implemented a (simple) python generator. Now, I want to make another one (from it), which will iterate through all values, but last one.

def gen(x):                   # Generate the interval [x, 10]
    if x <= 10:
        yield x
        for v in gen(x + 1):
            yield v

What would be the best way to accomplish that? Is it possible to alter the original generator using a decorator?

  • 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-06-16T00:44:07+00:00Added an answer on June 16, 2026 at 12:44 am

    Using a temporary variable as a simple “queue”, you can forward elements from any iterator and keep the last one at the end:

    def islice_to_last(it):
        prev_value = next(it)
        for value in it:
            yield prev_value
            prev_value = value
    

    or more generally (for generators up to x last elements):

    from collections import deque
    import itertools as it
    
    def islice_without_end(it, x):
        d = deque(it.islice(it, x))
        for value in it:
            yield d.popleft()
            d.append(value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple map-reduce type algorithm, which I want to implement in python and
I implemented a class in python which currently inherits from dict, but really, I
I implemented a simple C# application which inserts about 350000 records into the database.
I have implemented a simple linux shell in c. Now, I am adding some
I wrote a (not-so-)simple websocket server in python, which listens in a port, performs
I have a client written in Python for a server, which functions through LAN.
All I want to do is make some RPC calls over sockets. I have
I've implemented some simple parallelism in a Monte Carlo code using the Python multiprocessing
I'm looking for a simple, high level library (implemented in python would be nice)
I'm looking for a very simple way, from Python, to get information about Java

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.