Q:Pythonic way to create list of lists

Tim Chase python.list at tim.thechases.com
Sat Apr 11 16:25:36 EDT 2009


> 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.

Not using Netbeans, I can't verify that the below will stop the 
warning.  However the variable name "_" is used conventionally to 
mean "this doesn't matter".  Additionally, the starting point for 
xrange defaults to 0, so you can omit it.  That would translate to

  my_list = [[] for _ in xrange(10)]

-tkc





More information about the Python-list mailing list