0

I have a list of arrays with each array shape (2, 2). I want to "divide" them by rows so that each row is added to a new list of (1, 2).

Example:

X = np.random.rand(2, 2)
Y = np.random.rand(2, 2)
li = [X, Y]
li
[array([[0.06959048, 0.14011019],
        [0.8171005 , 0.31335564]]),
 array([[0.94952885, 0.15987638],
        [0.8033848 , 0.56760287]])]

I want this result:

new_li
[array([0.06959048, 0.14011019]),
 array([0.8171005 , 0.31335564]),
 array([0.94952885, 0.15987638]),
 array([0.8033848 , 0.56760287])]

I know that extend for each variables will do the desired result, e.g.:

li.extend(X)
li.extend(Y)

But I have a pre-existing list.

1 Answer 1

1

Use the following:

new_li = []
new_li.extend(j for i in li for j in i)

This will split each row and then extend all.

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.