[Tutor] List Comprehension (shared references)

Sean 'Shaleh' Perry shalehperry@attbi.com
Fri Apr 25 01:01:09 2003


On Thursday 24 April 2003 20:49, Tim Johnson wrote:
> Hi all:
> I'm just getting my feet wet with list comprehensions.
> I need to write a function that returns a nested list
> of empty lists.
>
> The following console session seems to fit the bill.
> x = 7
>
> >>> ml = [[] for row in range(x)]
> >>> ml
>
> [[], [], [], [], [], [], []]
>

[] generates a new list each time, so it is indeed safe.