Q:Pythonic way to create list of lists

G

grkuntzmd

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

Tim Chase

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
 
T

Terry Reedy

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.

Never heard of netbeans, but a decent code checker should have a
mechanism to annotate (comment) code to say "I know what I am doing
here, shut up".
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.

Adding something stupic like 'item = item' in the loop might 'work' ;-)

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

Right. Typical newbie mistake. Good for you for discovering that yourself.
Do I have to live with the warning, or is there a "better" way?

Different checker? PyChecker? PhLint?

tjr
 
A

Arnaud Delobelle

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.

IMHO this is the best way.
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?

You could do this:
[[], [], [], [], [], [], [], [], [], []]

But it is wasteful as it creates two lists.

There are some contrived ways to go around this using using itertools,
e.g.
[[], [], [], [], [], [], [], [], [], []]

Note that if you use Python 3 you would get an iterator so it would need
to be wrapped in a list() call. Or you could take advantage of
iter(function, sentinel) which is little used:
[[], [], [], [], [], [], [], [], [], []]

But none of these are very compelling.
 
T

Tino Wildenhain

I am just learning Python.

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

Apart from the solutions shown, why would you do that in the first
place?

Creating such a structure is probably not so pythonic so the way to
create it does not matter.

Sparse datastructures might be better represented with dicts.

Regards
Tino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top