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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:18:29+00:00 2026-06-13T05:18:29+00:00

This is more a programming exercise than a real-world problem: I am looking for

  • 0

This is more a programming exercise than a real-world problem: I am looking for a generator expression that resembles the behavior of append.

Consider:

def combine(sequence, obj):
    for item in sequence:
        yield item
    yield obj

s = ''.join(combine(sequence, obj))

This generator basically resembles append. In the workflow of my program the above is as fast as

sequence.append(obj)
s = ''.join(sequence)

I am now wondering if there is a neat generator expression genexpr with

s = ''.join(genexpr)

that resembles the append behavior above without performance caveats.

s = ''.join(_ for a in [sequence, [obj]] for _ in a)

performs bad.

  • 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-13T05:18:30+00:00Added an answer on June 13, 2026 at 5:18 am

    Try using chain from itertools module:

    ''.join(chain(sequence, [obj]))
    

    If you don’t want to create a new list for obj, then you may try this:

    ''.join(chain(sequence, repeat(obj,1)))
    

    I would use [obj] as it’s more readable and I doubt that repeat iterator has a less overhead than list creation.

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

Sidebar

Related Questions

This is more of a maths question than programming but I figure a lot
Excuse if this is more a file format conversion question rather than programming, but
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
This is semi-programming related. I'm working with more than 1 person on a website.
This is not really a programming question, more of an algorithmic one. The problem:
This is primarily a question of possibilities more than instructions. I'm a programming consultant
this is more OS architecture question than programming directly, but still. Why was the
So this is IT more than programming but Google found nothing, and you guys
This is more of a theory question than a programming question. As you know
This is more of a business-oriented programming question that I can't seem to figure

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.