2

I have a list X. I want to append elements of X[0][0],X[1][0],X[2][0], X[1][0],X[1][1],X[2][1] and so on. I present the current and expected outputs.

X=[[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]]
X1=[]
for i in range(0,len(X)):
    for j in range(0,len(X[0])):
        X1=X[i][j]
        X1.append(X)

The current output is

[1,
 [[[1, [...]], [2, [...]], [3, [...]], [4, [...]], [5, [...]]],
  [[8, [...]], [4, [...]], [6, [...]], [2, [...]], [3, [...]]],
  [[9, [...]], [1, [...]], [2, [...]], [5, [...]], [...]]]]

The expected output is

[[1,8,9], [2,4,1], [3,6,2], [4,2,5], [5,3,1]]
2
  • You probably want to do X1.append(X[i][j]) & remove the line just above that Commented Sep 12, 2022 at 13:46
  • Unfortunately X1.append(X[i][j]) yields [[1], [2], [3], [4], [5], [8], [4], [6], [2], [3], [9], [1], [2], [5], [1]] which is not the expected output. Commented Sep 12, 2022 at 13:49

5 Answers 5

4

Iterate over the sublists using zip, then add each inner lists, you can use list-comprehension for it:

>>> [x+y+z for (x,y,z) in zip(X[0], X[1], X[2])]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]

Or you can use reduce from functools and pass unpacked list to zip if you are not sure how many sublists are there

>>> from functools import reduce 
>>> [reduce(lambda x,y: x+y, v) for v in zip(*X)]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]

Another approach is to call sum iterating the unpacked list passed to zip as suggested by @Mad Physicist in the comment:

>>> [sum(x, start=[]) for x in zip(*X)]

[[1, 8, 9], [2, 4, 1], [3, 6, 2], [4, 2, 5], [5, 3, 1]]
Sign up to request clarification or add additional context in comments.

1 Comment

@user19977266: [sum(x, start=[]) for x in zip(*X)]
2

You can use numpy instead of dealing with loops

import numpy as np
array = np.array([[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]])
// reshape and transpose
array = array.reshape(3,5).T


array will be

array([[1, 8, 9],
       [2, 4, 1],
       [3, 6, 2],
       [4, 2, 5],
       [5, 3, 1]])

1 Comment

You might want to determine the shape (3, 5) dynamically from the input list.
0

Numpy can help a lot here. First when get rid of the inner-most single-item lists, turning the three list[list[int]] into three list[int]. Then, those can be used to create a np.ndarray. We then transpose this array to get the desired result.

import numpy as np

X = [[[1], [2], [3], [4], [5]],
     [[8], [4], [6], [2], [3]],
     [[9], [1], [2], [5], [1]]]

a = [[item[0] for item in sublist] for sublist in X]
# [[1, 2, 3, 4, 5], [8, 4, 6, 2, 3], [9, 1, 2, 5, 1]]

a = np.array(a).T
# array([[1, 8, 9],
#        [2, 4, 1],
#        [3, 6, 2],
#        [4, 2, 5],
#        [5, 3, 1]])

1 Comment

If you're going to use numpy, why are you using loops instead of plain indexing?
0

If there are fewer and more than 3 elements:

import numpy as np
x=[[[1], [2], [3], [4], [5]], [[8], [4], [6], [2], [3]],[[9], [1], [2], [5], [1]]]
 
x = np.asarray(x)
x = x.reshape(x.shape[0],x.shape[1]).T
x

Output for(3,5,1):

array([[1, 8, 9],
       [2, 4, 1],
       [3, 6, 2],
       [4, 2, 5],
       [5, 3, 1]])

Output for (4,5,1):

array([[1, 8, 9, 9],
       [2, 4, 1, 1],
       [3, 6, 2, 2],
       [4, 2, 5, 5],
       [5, 3, 1, 1]])

Comments

0

As your list (X) contains a few lists with the same size, it's number of elements can be just indexes_per_list = len(X[0]). We can then search for the index in every sub-list, and append the correct index. In order to remove the square brackets, we just use explode (*):

output = []
indexes_per_list = len(X[0])

for i in range(indexes_per_list):
    item = []
    
    for l in X:
        item.append(*l[i])
    
    output.append(item)

print(output)

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

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.