defining multi dimensional array

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jul 5 01:55:11 EDT 2006


On Tue, 04 Jul 2006 22:07:59 -0700, bruce wrote:

> do i need to import anything for this.. or is it supposed to work out of the
> box..

Why don't you try it, and see if it gives you a result or raises an
exception? The exception (if any) will give you a clue what you need to do.

> and just what is it doing!!!!!

The original list comprehension is:

[[None for x in xrange(10)] for y in xrange(10)]

This is a list comprehension inside another list comprehension. Start from
the inside and work out. The inside list comp is:

[None for x in xrange(10)]

That is equivalent to:

result = []
for x in xrange(10):
    result.append(None)

and it creates a list of ten None items.

The outer list comp is 

[list_comp for y in xrange(10)]

where list_comp is the inner list comprehension. It also creates a list
of ten items, where each item is [None,None,....None]. But the important
thing is that each of the inner lists are DIFFERENT lists that just happen
to share the same value (ten None items), rather than the same list
repeated ten times.

-- 
Steven.




More information about the Python-list mailing list