Multi-dimensional list initialization

Ian Kelly ian.g.kelly at gmail.com
Tue Nov 6 23:46:52 CET 2012


On Tue, Nov 6, 2012 at 2:36 PM, Andrew Robinson
<andrew3 at r3dsolutions.com> wrote:
> I meant all lists are shallow copied from the innermost level out.
> Equivalently, it's a deep copy of list objects -- but a shallow copy of any list contents except other lists.

Why only list objects, though?  When a user writes [[]] * 10, they
probably want a list containing ten distinct nested lists.  Likewise,
when a user writes [{}] * 10, they probably want a list containing ten
distinct dicts, which is not at all an uncommon thing to want.  It
seems very inconsistent that the former should work while the latter
should not.  This is especially true when you start mixing the two
paradigms; the user might expect [[{}] * 10] * 10 to create a a 10x10
matrix where each element is a distinct dict, but this still would not
work, even though the nested lists would all have different
identities.

What about ([],) * 10?  This is perhaps best interpreted as a request
to create a matrix of ten rows where the rows themselves are mutable
but the collection of rows is not.  If list multiplication were to
copy nested lists, then should tuple multiplication do the same?



More information about the Python-list mailing list