0

I have a generator which iterates over strings, eg ['apple', 'banana', orange']. I have an additional generator which iterates over a different set of strings, eg ['car', 'truck', 'bike']

I'd like to create a generator, that when iterated over, returns the following: 'applecar', 'bananatruck', 'orangebike'.

Is there a way to do this?

2
  • 1
    Use the zip function: [a+b for a,b in zip(list1, list2)] Commented Oct 13, 2020 at 4:56
  • @Mike67, you have permission to post answers--no need to put it in a comment Commented Oct 13, 2020 at 4:58

1 Answer 1

1

Yes, but make sure to think about what you want to happen if the two are different lengths.

def generator(a, b):
    for a_element, b_element in zip(a,b):
        yield a_element + b_element

or more compactly:

new_gen = ( a_element + b_element for a_element,b_element in zip(a,b) )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.