0

I have an array a as follow : [[(0,0),(2,0)],[(1,1)], [(3,8)]]

So now I want to convert it like this: [(0,0),(2,0),(1,1), (3,8)]

How may I do that?

I had tried bellow code and successed, but I need some ideas better and faster.

nresult = []
for i in range(len(result)):
    arr = result[i]
    for j in range(len(arr)):
        nresult.append(arr[j])

Can someone help me?

Thanks!

2

3 Answers 3

1

You can use reduce from functools like this

from functools import reduce

a = [[(0,0),(2,0)],[(1,1)], [(3,8)]]
res = reduce(lambda x,y: x+y,a)

print(res) # [(0, 0), (2, 0), (1, 1), (3, 8)]
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to hear I could help. :)
1

If your nested-deep is certain, you can use chain from itertools package

from itertools import chain

data = [[(0,0),(2,0)],[(1,1)], [(3,8)]]

result = list(chain(*data))

Comments

0

You can use list comprehensions -

nested = [[(0,0),(2,0)],[(1,1)], [(3,8)]]
un_nested = [inside_element for element in nested for inside_element in element]
# Returns - [(0, 0), (2, 0), (1, 1), (3, 8)]

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.