List behaviour

Lie Lie.1296 at gmail.com
Thu May 15 11:48:06 EDT 2008


On May 15, 5:08 pm, Gabriel <g... at dragffy.com> wrote:
> Hi all
>
> Just wondering if someone could clarify this behaviour for me, please?
>
> >>> tasks = [[]]*6
> >>> tasks
>
> [[], [], [], [], [], []]>>> tasks[0].append(1)
> >>> tasks
>
> [[1], [1], [1], [1], [1], [1]]
>
> Well what I was expecting to end up with was something like:
> >>> tasks
> [[1], [], [], [], [], []]
>
> I got this example from page 38 of Beginning Python.
>
> Regards
>
> Gabriel

As a complementary note, if you want to copy a list (probably a filled
one instead of blank ones like here) and wanted to copy a list so that
the new copy is a completely separate instance from the old copy, you
may use deepcopy (from copy import deepcopy).



More information about the Python-list mailing list