2

Im trying to create a number of lists depending on the number in my header_count. The code below should generate 3 lists but i get a syntax error instead.

header_count = 4
for i in range(1, header_count):
    header_%s = [] % i
Harpal
  • 12,057
  • 18
  • 61
  • 74
  • If you're going to generate 3 lists, it's very strange to set `header_count` to 4. Better to set `header_count = 3` and iterate `for i in xrange(header_count)`. – David Z Mar 18 '11 at 23:10

5 Answers5

13

This is my interpretation of what you want, I hope I guessed it right (you weren't very clear).

header_count = 4
headers = [[] for i in range(1, header_count)]

Now you can use it like this:

headers[1].append("this goes in the first header")
headers[2].append("this goes in the second header")
r12
  • 1,712
  • 1
  • 15
  • 25
orlp
  • 112,504
  • 36
  • 218
  • 315
  • Im trying to create three lists indivually named. So I need header_1, header_2 and header_3 as lists – Harpal Mar 18 '11 at 22:48
  • Why? That really is a bad practice. This way you use it as `header[0]`, `header[1]`, `header[2]`. – orlp Mar 18 '11 at 22:50
4

What you want is to to create a list of lists:

header_count = 4
header = []
for i in range(header_count):
    header[i] = []

In the header variable references a list containing 4 lists. Each list can be accessed as follow:

header[0].append(1)
header[1].append("Hi")
header[2].append(3.14)
header[3].append(True)
vz0
  • 32,345
  • 7
  • 44
  • 77
1

If you need list names (as it seems from your comment to nightcracker answer), you can use a dictionary of lists:

header_count = 4
listDict = {}
for i in range(1, header_count):
    listDict["header_"+str(i)] = []

Then you can access the dictionary using header_1, header_2, header_3 as keys.

Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
  • Please don't encourage bad practices. – orlp Mar 18 '11 at 22:59
  • That's what he seemed to have asked for! One can never know, maybe it was a curiosity even if it's a terrible practice. – Vincenzo Pii Mar 18 '11 at 23:03
  • A dictionary is the appropriate data structure if the names are dynamically generated and have some information content beyond a simple counter (see the answer to the following question [how-can-you-dynamically-create-variables-in-python-via-to-a-while-loop](http://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-in-python-via-to-a-while-loop)). If the variables names are redundant with a simple count or index, then a list is the appropriate data structure. – JoshAdel Mar 19 '11 at 00:23
1

You can use global() to create these variables:

header_count = 4
for i in range(1, header_count):
    globals()[f"header_{i}"] = []

Note: you started your range with value 1, so you will create three empty lists:

header_1
header_2
header_3
Rodolfo Bugarin
  • 605
  • 6
  • 12
0

What did you mean by header_%s? % is the mod operator, of course you can't assign to an expression involving an operator. It's like writing

a+b = c

You can't assign to a+b, nor can you assign to header_%s.

Did you mean this?

header_lists = [[] for i in range(1,header_count)]
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141