Patrick Patrick - 6 months ago 14
Python Question

Initialising an n-length tuple of lists

So in Python, if I wanted to create a list of lists I could do something like:

[[] for _ in range(list_length)]


And to create a tuple of lists of a similar nature I could technically write:

tuple([[] for _ in range(list_length)])


But is this computationally inefficient?

Is there a more efficient, simpler way to create a tuple of lists of length 'n'?

Answer

Use a genex instead of a LC.

tuple([] for _ in range(list_length))