3

I have generated the results into the list like this

In[11]: a

Out[11]: [[[1, 3, 2, 4], [1, 3, 2, 6]],
         [[2, 4], [2, 6]],
         [[3, 2, 4], [3, 2, 6]],
         [[4]],
         [[5, 4]],
         [[6]]]

but I would like to remove the square brackets that are leftover resulting as this

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

I have tried sum(a,[]) to reduce 1 dimension but the result is shown as follows

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

and tried np.squeeze(a) but the result is shown as follows

array([list([1, 3, 2, 4]), list([1, 3, 2, 6]), list([2, 4]), list([2, 6]),
       list([3, 2, 4]), list([3, 2, 6]), list([4]), list([5, 4]),
       list([6])], dtype=object)

any suggestions for aggregating this kind of list?

thank you in advance

3
  • 2
    sum(a,[]) worked on my machine. check. Commented Mar 17, 2020 at 4:53
  • Yes. In my machine too. Not sure why it didn't work in user's machine. Commented Mar 17, 2020 at 4:55
  • i don't know the problem with this issue too. thank you for your comment! Commented Mar 17, 2020 at 5:02

3 Answers 3

4

You can try this below :

    a = [[[1, 3, 2, 4], [1, 3, 2, 6]],
         [[2, 4], [2, 6]],
         [[3, 2, 4], [3, 2, 6]],
         [[4]],
         [[5, 4]],
         [[6]]]
    output = [elem for output_list in a for elem in output_list]
    print(output)
Sign up to request clarification or add additional context in comments.

Comments

2

Try this.

a = [[[1, 3, 2, 4], [1, 3, 2, 6]],
     [[2, 4], [2, 6]],
     [[3, 2, 4], [3, 2, 6]],
     [[4]],
     [[5, 4]],
     [[6]]]

fl = []
for i in a:
    for j in i:
        fl.append(j)

print fl

Comments

0
str(a).replace('[[','[').replace(']]',']')

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.