List assignment, unexpected result

Paul Rubin phr-n2002b at NOSPAMnightsong.com
Wed Jul 10 20:45:22 EDT 2002


"Steve Coates" <steve.coates at virgin.net> writes:
> Thanks for the help. I think my favourite solution was
> 
> grid = [['.' * 4] for i in range(4)]
> 
> but I'll need to read up on the syntax. I haven't seen this
> use of a for loop before.

That's not a for loop, it's a new Python gizmo called a list
comprehension.  List comprehensions were introduced in Python 2.0.
You can find more info about list comprehensions in the Python docs.
But if your script has to work under earlier Python versions than 2.0,
don't use them.



More information about the Python-list mailing list