Creating a matrix?

Mike C. Fletcher mcfletch at rogers.com
Fri Apr 2 10:27:52 EST 2004


N*N matrix of lists...

    [ [0]*N for i in range( N ) ]

Seems clearer to me, anyway (and stores actual 0 values, not '0' in the 
result).  Have fun,
Mike

Ivan Voras wrote:

> Is there a nice(r) way of creating a list of uniform values? I'm 
> currently using: list('0'*N), which makes a string and then chops it 
> up into a list. I need it to create a NxN matrix:
>
> matrix = [list('0'*N) for i in range(N)]
>
> (elements need to be mutable afterwards, a shallow copy is bad)
>
> While this is short and concise, it also feels odd :)

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/






More information about the Python-list mailing list