List behaviour

Gabriel gabe at dragffy.com
Thu May 15 06:50:47 EDT 2008


Bruno Desthuilliers <bruno.42.desthuilliers <at> websiteburo.invalid> writes:


> The problem here is that your first statement
> 
> #>>> tasks = [[]]*6
> 
> creates a list (task) containing 6 references to the *same* (empty) list 
> object. You can check this easily using the identity test operator 'is':
> 

> If you want 6 different list objects in tasks, you can use a list 
> comprehension instead:
> 
> #>>> tasks = [[] for i in range(6)]
> #>>> tasks
> [[], [], [], [], [], []]
> #>>> tasks[0].append(1)
> #>>> tasks
> [[1], [], [], [], [], []]
> 
> HTH
> --
> http://mail.python.org/mailman/listinfo/python-list

Hi Bruno

Thanks for clearing that up. Your example certainly works well, I will study
list comprehension a little more.

Many thanks

Gabriel







More information about the Python-list mailing list