This is not the best way to create nested lists. Don't do it in your code. Do this instead. However, I'm curious about this way only.
Out of curiosity. I tried googling but all I got is how to do it and not why, and I already know that part.
I was trying to instantiate the following structure with a multiplier:
[[], [], []]
from an unknown number of inner lists, so I wanted to multiply the lists by range(len(other_list))
(this last part is unrelated, so I´ll simplify multiplying by 3 here).
After failing intuitively, with the following expression: [[]] *3
, I learned in another question that you need to represent as multiplying both lists (inner ones and one outer each) instead.
>>[[]] * 3
[[], [], []]
>>[[] * 3]
[[]]
- Why the sentence works that way (it's kind of multiplying the double list first and then doing and extend of the outer list...?)
- Python is usually very intuitive in all its expressions. What makes this one different?
Thanks for the patience. I know this is a weird question :D