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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:13:04+00:00 2026-05-20T11:13:04+00:00

Suppose I have an iterator, and I want to add some elements before or

  • 0

Suppose I have an iterator, and I want to add some elements before or after it. The only way I can think of to do this is to use an explicit loop:

def myiter(other_iter):
    yield "First element"
    for item in other_iter:
        yield item
    yield "Last element"

Is there a better or more efficient way to do this? Is there a function with a name like yield_items_from that can be used like this?

def myiter(other_iter):
    yield "First element"
    yield_items_from(other_iter)
    yield "Last element"

Edit:

Ok, I oversimplified my example. Here’s a better one:

Suppose I have an iterator other_iter that returns an ascending sequence of nonnegative integers. I want to return an iterator that counts up from zero, returning 1 for numbers returned by other_iter and 0 otherwise. For example, if other_iter yields [1,4,5,7], I want to yield [0,1,0,0,1,1,0,1]. Is there an efficient and readable way to do this?

  • 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-20T11:13:05+00:00Added an answer on May 20, 2026 at 11:13 am

    No, there is nothing like yield_items_from, although there is a draft proposal for adding one to Python 3.X in PEP 380.

    For current Python, the explicit loop is the only way to yield from a sub-iterator. However, it’s reasonably efficient, and it is the idiomatic way to do it, so you shouldn’t be too bothered by the verbosity.

    If all you need to do is add new items to the front or back of the iterator, then you can simply use itertools.chain to create a new iterator. You can use it to chain several iterators together, or append/prepend individual items by wrapping them in a list.

    new_iter = itertools.chain(['prefix item'], old_iter, appended_iter)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose we have an iterator (an infinite one) that returns lists (or finite iterators),
Suppose I have a hash_map and a code like // i is an iterator
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a STL map where the values are pointers, and I want
Suppose I have a vector<int> myvec and I want to loop through all of
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have a list of sets and I want to get the union

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.