Multi-dimensional list initialization

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Nov 7 17:15:17 CET 2012


On Nov 7, 2012 3:55 PM, "Ethan Furman" <ethan at stoneleaf.us> wrote:
>
> Oscar Benjamin wrote:
>>
>> A more modest addition for the limited case described in this thread
could be to use exponentiation:
>>
>>  >>> [0] ** (2, 3)
>> [[0, 0, 0], [0, 0, 0]]
>
>
> What would happen with
>
> --> [{}] ** (2, 3)

The list being exponentiated does nothing with its elements. The
exponentiation just tells it to create a list of distinct lists. In this
case each element of each sublist is the same dict. However if you assign
to an element of the sublist (rather than into the dict) it replaces the
dict in that sublist and not the others.

>
> or
>
> --> [my_custom_container()] ** (2, 3)

Ditto

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121107/5c4d169d/attachment.html>


More information about the Python-list mailing list