7

I would like to convert the tuple of tuples into a list of lists.

I don't want to flatten it, unlike these questions, and I don't want to make it a numpy array like in this question.

My only idea so far is to iterate the tuples with for loops and copy the values, but there must be something cleaner and more pythonic.

Community
  • 1
  • 1
Pavel V.
  • 2,653
  • 10
  • 43
  • 74

1 Answers1

18

Is this what you want? -

In [17]: a = ((1,2,3),(4,5,6),(7,8,9))

In [18]: b = [list(x) for x in a]

In [19]: b
Out[19]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This is called list comprehension, and when you do list(x) where x is an iterable (which also includes tuples) it converts it into a list of same elements.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176