0

Consider the following code:

def square(x):
    return x**2


def cube(x):
    return x**3


in1 = [0, 2, 3]
in2 = [2, 1, 9]

y1 = map(square, in1)
y2 = map(cube, in2)

How to concatenate the two maps objects without explicitly converting the iterators y1 and y2 into iterables?

1
  • 1
    "convert into iterables" doesn't really make sense, as they're already iterables. Commented Oct 7, 2023 at 12:59

1 Answer 1

2

You can use itertools.chain

import itertools
def square(x):
    return x**2


def cube(x):
    return x**3


in1 = [0, 2, 3]
in2 = [2, 1, 9]

y1 = map(square, in1)
y2 = map(cube, in2)

y1_y2 = itertools.chain(y1, y2)

print(list(y1_y2))
# [0, 4, 9, 8, 1, 729]

Related answer: How to extend/concatenate two iterators in Python

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.