Q:Pythonic way to create list of lists

grkuntzmd at gmail.com grkuntzmd at gmail.com
Sat Apr 11 22:11:42 CEST 2009


I am just learning Python.

I am trying to create a list of empty lists: [[], [], [], ...] (10
items total).

What is the most Pythonic way to do this?

If I use a list comprehension (as in myList = [[] for item in xrange
(0, 10)]), Netbeans warns me that 'item' is never used.

If I use a for-loop (as in for item in myList = []; for item in xrange
(0, 10): myList.append([])), Netbeans still warns me of the same
thing.

If I use '*' (as myList = [[]] * 10), all of the empty lists refer to
the same object; changing one changes them all.

Do I have to live with the warning, or is there a "better" way?

Thanks.



More information about the Python-list mailing list