how to make a nested list
gherron at digipen.edu
Thu Sep 15 13:28:23 EDT 2011
On 09/15/2011 09:57 AM, Stef Mientki wrote:
> I need a nested list, like this
> >>> A= [ [None,None], [None,None], [None, None] ]
> >>> A =77
> >>> A
> [[None, None], [None, None], [77, None]]
> Because the list is much larger, I need a shortcut (ok I can use a for
> So I tried
> >>> B = 3 * [ [ None, None ]]
> >>> B = 77
> >>> B
> [[77, None], [77, None], [77, None]]
> which doesn't work as expected.
> any suggestions ?
A for loop to fill in the array would work, but list comprehension
(which is really just an internal loop for filling an array) is probably
more efficient, certainly less code, and arguably more Pythonic.
A = [ [None,None] for i in range(1000) ]
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
More information about the Python-list