How do I initialize a list please?

Mike C. Fletcher mcfletch at rogers.com
Tue Dec 31 17:46:26 EST 2002


Well, although it's less compact, I kinda like this approach for 
list-initialisation w/ list comps because it uses the same paradigm 
everywhere (instead of mults in one place and comps in another). 
 Downside is that it's more verbose, and may freak some of the peanut 
gallery out ;) .  When you come to change the comprehension to do 
something more (such as include i or j in the result), it's just a 
matter of changing the output fragment...

newstructure = [
    [ 1.0  for i in range(10) ]
    for j in range(10)
]

Oh well, TMTOWTDI... oh, darn, wrong language,
Mike

Christian Tismer wrote:
...

>
> Semantically equivalent, but probably the standard
> way for Python with list comprehensions, you would use
>
> >>> dim = [ [1.0] * 10 for i in xrange(10) ]
>
> Happy new year -- chris
>
...
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/







More information about the Python-list mailing list