tags:

views:

106

answers:

2

I am looking for a convenient way to create a list of lists for which the lists within the list have consecutive numbers. So far I only came up with a very unsatisfying brute-typing force solution (yeah right, I just use python for a few weeks now):

block0 = []
...
block4 = []

blocks = [block0,block1,block2,block3,block4]

I appreciate any help that works with something like nrBlocks = 5.

+2  A: 

It's not clear what consecutive numbers you're talking about, but your code translates into the following idiomatic Python:

[[] for _ in range(4)]          # use xrange in python-2.x
SilentGhost
I wanted to access the sublists via name (i.e., block3), but after thinking about it a second time i realize that accessing them as block[3] is basically the same. As each sublist still can have a different length and its not an array as in e.g., C.
Henrik
Even in C, you could have an array of pointers to differently-sized arrays.
Devin Jeanpierre
You're off by one. You want range(5)
Jason R. Coombs
@Jason: question was edited.
SilentGhost
A: 

Don't do it this way. Put it in blocks in the first place:

blocks = [
  [ ... ],
  [ ... ],
  [ ... ],
  [ ... ]
]
Ignacio Vazquez-Abrams