6

I want to convert tuple like

t = [(4,10),(9,7),(11,2),(2,2)]

into 2D array like:

a = [[4,10],[9,7],[11,2],[2,2]]

I tried

a = []
for i in t:  
    a.append(np.asarray(i))
print a

is there any easier way?

3
  • 1
    a = map(list, t) Py2 , map changes in Py3 and you need a = list(map(list, t)) for Py3 Commented Jan 17, 2016 at 17:15
  • @AChampion As OP is using Python 2.7, I think there is no need to cast the map()result to a list. Commented Jan 17, 2016 at 17:17
  • Thanks, I was in the process of fixing - I work almost exclusively in 3 now so tend to forget these anachronisms. Commented Jan 17, 2016 at 17:18

1 Answer 1

9

Use a list comprehension as follows:

>>> t = [(4,10),(9,7),(11,2),(2,2)]
>>> [list(item) for item in t]
[[4, 10], [9, 7], [11, 2], [2, 2]]
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.