how to make a nested list

Gary Herron gherron at digipen.edu
Thu Sep 15 19:28:23 CEST 2011


On 09/15/2011 09:57 AM, Stef Mientki wrote:
> hello,
>
> I need a nested list, like this
>
> >>> A= [ [None,None], [None,None], [None, None] ]
> >>> A[2][0] =77
> >>> A
> [[None, None], [None, None], [77, None]]
>
>
> Because the list is much larger, I need a shortcut (ok I can use a for 
> loop)
> So I tried
> >>> B = 3 * [ [ None, None ]]
> >>> B[2][0] = 77
> >>> B
> [[77, None], [77, None], [77, None]]
>
> which doesn't work as expected.
>
> any suggestions ?
>
> thanks,
> Stef

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


-- 
Gary Herron, PhD.
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list