Henrik
Henrik

Reputation: 14460

Creating a list of lists with consecutive numbers

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.

Upvotes: 3

Views: 5518

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799580

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

blocks = [
  [ ... ],
  [ ... ],
  [ ... ],
  [ ... ]
]

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 320049

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

Upvotes: 5

Related Questions